3

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 ?

Paul Kendal
  • 559
  • 9
  • 24
  • Have your searched for this particular problem? It seems not at all. There are a myriad of results on Google but even here on SO you will find a number of answers all saying the same. – marekful Aug 12 '15 at 15:24
  • possible duplicate of [Prettifying/Formatting output in SimpleXML](http://stackoverflow.com/questions/10133157/prettifying-formatting-output-in-simplexml) – marekful Aug 12 '15 at 15:25
  • i did indeed. i tried this; $sxe->asXML('simple_xml_create.xml'); $dom = new DOMDocument('1.0'); $dom->preserveWhiteSpace = false; $dom->formatOutput = true; $dom->loadXML($sxe->asXML()); echo $dom->saveXML(); but it does not work – Paul Kendal Aug 12 '15 at 15:41
  • Possible duplicate of [PHP simpleXML how to save the file in a formatted way?](http://stackoverflow.com/questions/798967/php-simplexml-how-to-save-the-file-in-a-formatted-way) – cweiske May 16 '17 at 11:55

1 Answers1

0

First of all you were successful in finding out that DOMDocument supports formatted XML while SimpleXMLElement does not. And that is totally correct.

So as you want to use SimpleXMLElement to actually create the XML document but you also want to benefit from the formatting options from DOMDocument you might want to learn about the fact that both libraries are sister-libraries that work very well with each other. You already sort of seem to know about that as you make use of simplexml_import_dom to convert a DOMNode object into it's SimpleXMLElement object.

You however seem to miss how near you already were. Please see the following example which I copied from your question - I just added a single line at the very end (and change the filename to standard output so it echoes out the documents):

$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('php://output');
$dom->save('php://output');

The SimpleXMLElement::saveXML() method will use non-formatted output while the DOMDocument::save() method will use the formatting as you set the parameters for:

<?xml version="1.0"?>
<user><firstname>John</firstname><surname>Brady</surname></user>
<?xml version="1.0"?>
<user>
  <firstname>John</firstname>
  <surname>Brady</surname>
</user>

And that's it already.

hakre
  • 193,403
  • 52
  • 435
  • 836