Dhiraj
Dhiraj

Reputation: 33628

remove xml version tag when a xml is created in php

I'm creating a xml using this

$customXML = new SimpleXMLElement('<abc></abc>');

after adding some attributes onto this, when I try to print it it appears like this,

<?xml version="1.0"?>
<abc id="332"><params><param name="aa">33</param></params></abc>

Is there a way to remove the xml version node ? Thank you

Upvotes: 37

Views: 40365

Answers (10)

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.

Upvotes: -1

Wahid Masud
Wahid Masud

Reputation: 1093

$customXML = new SimpleXMLElement('<source><abc>hello</abc></source>');
$result = $customXML->xpath('//abc');
$result = $result[0];
var_dump($result->asXML());

Upvotes: 0

CubFuzzy
CubFuzzy

Reputation: 21

echo preg_replace("/<\\?xml.*\\?>/",'',$doc->saveXML(),1);

Upvotes: 2

Soliman
Soliman

Reputation: 441

Try:

$xmlString = $doc->saveXML();
$xmlString = str_replace("<?xml version=\"1.0\"?>\n", '', $xmlString);
file_put_contents($filename, $xmlString);

Upvotes: 6

hakre
hakre

Reputation: 198204

As SimpleXMLElement always uses "\n" to separate the XML-Declaration from the rest of the document, it can be split at that position and the remainder taken:

explode("\n", $customXML->asXML(), 2)[1];

Example:

<?php

$customXML = new SimpleXMLElement('<!-- some comment -->
<abc>
</abc>');

echo explode("\n", $customXML->asXML(), 2)[1];

Output:

<!-- some comment -->
<abc>
</abc>

Upvotes: 7

Jon
Jon

Reputation: 437684

A practical solution: you know that the first occurrence of ?> in the result string is going to be then end of the xml version substring. So:

$customXML = new SimpleXMLElement('<abc></abc>');
$customXML = substr($customXML, strpos($customXML, '?'.'>') + 2);

Note that ?> is split into two parts because otherwise some poor syntax highlighter may have problems parsing at this point.

Upvotes: 13

hrvoj3e
hrvoj3e

Reputation: 2754

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

Upvotes: 17

Hitman_99
Hitman_99

Reputation: 2282

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>

Upvotes: 6

Gordon
Gordon

Reputation: 317177

In theory you can provide the LIBXML_NOXMLDECL option to drop the XML declaration when saving a document, but this is only available in Libxml >= 2.6.21 (and buggy). An alternative would be to use

$customXML = new SimpleXMLElement('<abc></abc>');
$dom = dom_import_simplexml($customXML);
echo $dom->ownerDocument->saveXML($dom->ownerDocument->documentElement);

Upvotes: 48

pintxo
pintxo

Reputation: 2155

If this is a problem, this should do it:

$xml = str_replace(' version="1.0"', '', $xml);`

Upvotes: 0

Related Questions