Kaushal Prajapati Kaushal Prajapati - 3 months ago 15
PHP Question

Convert SimpleXMLElement object to XML format in php

one xml file view.xml

convert it into SimpleXMLElement Object after get parent node
it's child object need to convert into back in xml format in php

**

View.xml


**

> <cards name="country-cards" model="core_country" title="Country" >
> <field name="name" />
> <field name="code" />
> <template><![CDATA[
> <div class="span12"><strong>{{name}}</strong></div> <div>
> <div class="span8">
> <span>{{name}}<br/>
> {{code}}<br/></span>
> </div></div>]]>
> </template>
> </cards>

**$xml = simplexml_load_file($xmlViewFilePath);**



foreach ($xml as $key=>$value) {


$value want to convert back in to xml formate


}


QUESTION: Any other simple way to achieve SimpleXMLElement Object to XML parsing and store into Database without any I/O operation?

Already used the php conversion mechanism which results in output as null in database link below

http://us2.php.net/manual/en/simplexmlelement.asxml.php

Answer

Try Somthing like this..

 $doc = new DOMDocument();
    $doc->formatOutput = TRUE;
    $doc->loadXML($sxml->asXML());
    $xml = $doc->saveXML();

For more information check this link

http://us2.php.net/manual/en/simplexmlelement.asxml.php

Comments