i am converting php variables into XML format.
i noticed that when i just use the DOMDocument along with;
$dom->formatOutput = true;
to create the XML, it formats correctly (i.e line indenting etc)
code using just DOMDocument
$dom = new DOMDocument('1.0','UTF-8');
$dom->formatOutput = true;
$root = $dom->createElement('student');
$dom->appendChild($root);
$result = $dom->createElement('result');
$root->appendChild($result);
$result->setAttribute('id', 1);
$result->appendChild( $dom->createElement('name', 'Opal Kole') );
$result->appendChild( $dom->createElement('sgpa', '8.1') );
$result->appendChild( $dom->createElement('cgpa', '8.4') );
echo '<xmp>'. $dom->saveXML() .'</xmp>';
$dom->save('result.xml') or die('XML Create Error');
**The Result **
<?xml version="1.0" encoding="UTF-8"?>
<student>
<result id="1">
<name>Opal Kole</name>
<sgpa>8.1</sgpa>
<cgpa>8.4</cgpa>
</result>
</student>
However if i try using simplexml with the domDocument it no longer formats correctly ;
below is a sample of the problem
$dom = new domDocument;
$dom->formatOutput = true;
$root = $dom->appendChild($dom->createElement( "user" ));
$sxe = simplexml_import_dom( $dom );
$sxe->addChild("firstname", "John");
$sxe->addChild("surname", "Brady");
$sxe->asXML('testingXML.xml');
the result;
<?xml version="1.0"?>
<user><firstname>John</firstname><surname>Brady</surname></user>
you will note that it no longer formats in the desired way.
i have followed all the online tutorial examples that combine simple xml with the dom but none of them format correctly.
UPDATE
i tried many of the solution on the forumns but non seemed to work for me.
however,i have just found this solution- it does indeed work- however, its long winded. it requires me to first save the file using the simplyXML and to then reload the the saved file and NOW save it using the DocumentDom:
$sxe->asXML('simple_xml_create.xml');
$simplexml = simplexml_load_file("simple_xml_create.xml");
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($simplexml->asXML());
$xml = new SimpleXMLElement($dom->saveXML());
$xml->saveXML("simple_xml_create.xml");
IS THERE A MORE ELEGANT SOLUTION ?