Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php simplexml without xml declaration tag [duplicate]

Tags:

php

xml

simplexml

I use SimpleXML to generate XML. When I use :

new SimpleXMLElement('<row></row>');

SimpleXML generates this XML :

<?xml version="1.0"?>\n<row/>

But I need only :

<row/>

I need only it because I will include it to another XML.

How can I generate only row tag in an SimpleXML Object ?

like image 584
Samuel Dauzon Avatar asked Jan 15 '15 16:01

Samuel Dauzon


1 Answers

As has been outlined in the earlier Q&A:

  • remove xml version tag when a xml is created in php

You can import the SimpleXMLElement into the PHP Document Object Model (DOM) extension and output it there as XML because that extension can output without the XML declaration.

Importing into dom works with the dom_import_simplexml function.

Example:

$xml    = new SimpleXMLElement('<row></row>');
$domxml = dom_import_simplexml($xml);
echo $domxml->ownerDocument->saveXML($domxml->ownerDocument->documentElement);

Output:

<row/>

You can also run this code your own online here: https://eval.in/302096

Hope this helps you.

like image 54
Utkarsh Dixit Avatar answered Oct 16 '22 13:10

Utkarsh Dixit