PHP DomDocument output without <?xml version="1.0" encoding="UTF-8"?>

user398341 picture user398341 · Apr 18, 2011 · Viewed 19.9k times · Source

is there an option with DomDocument to remove the first line:

<?xml version="1.0" encoding="UTF-8"?>

The class instantiation automatically adds it to the output, but is it possible to get rid of it?

Answer

hrvoj3e picture hrvoj3e · Jun 28, 2013

I think using DOMDocument is a universal solution for valid XML files:

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