remove xml version tag when a xml is created in php

前端 未结 10 1830
星月不相逢
星月不相逢 2020-11-28 10:28

I\'m creating a xml using this

$customXML = new SimpleXMLElement(\'\');

after adding some attributes onto this, when

相关标签:
10条回答
  • 2020-11-28 11:16

    Try:

    $xmlString = $doc->saveXML();
    $xmlString = str_replace("<?xml version=\"1.0\"?>\n", '', $xmlString);
    file_put_contents($filename, $xmlString);
    
    0 讨论(0)
  • 2020-11-28 11:16

    XML header tags and PHP short tags are incompatible. So, this may be an issue of using the short tags in PHP (i.e. <?= instead of <?php echo...). So, turning off short tags in PHP.INI (and updating your code appropriately) may resolve this issue.

    0 讨论(0)
  • 2020-11-28 11:18

    I have a simmilar solution to the accepted answer:

    If you have xml allready loaded in a variable:

    $t_xml = new DOMDocument();
    $t_xml->loadXML($xml_as_string);
    $xml_out = $t_xml->saveXML($t_xml->documentElement);
    

    For XML file from disk:

    $t_xml = new DOMDocument();
    $t_xml->load($file_path_to_xml);
    $xml_out = $t_xml->saveXML($t_xml->documentElement);
    

    This comment helped: http://www.php.net/manual/en/domdocument.savexml.php#88525

    0 讨论(0)
  • 2020-11-28 11:18

    There's another way without the replacing xml header. I prefer this:

    $xml = new xmlWriter();
    $xml->openMemory();
    $xml->startElement('abc');
    $xml->writeAttribute('id', 332);
    $xml->startElement('params');
    $xml->startElement('param');
    $xml->writeAttribute('name', 'aa');
    $xml->text('33');
    $xml->endElement();
    $xml->endElement();
    echo $xml->outputMemory(true);
    

    Gives output:

    <abc id="332"><params><param name="aa">33</param></params></abc>
    
    0 讨论(0)
提交回复
热议问题