0

I'm start to learn how to use the domDocument and SimpleXML classes to create and parse xml in PHP.

When reading through the domDocument class definition, I see a property for formatOutput that says it:

Nicely formats output with indentation and extra space.

I added some example code and set formatOutput to true:

<?php

try{
    $dom = new domDocument("1.0");
    $dom->preserveWhiteSpace = false;
    $dom->formatOutput = true;

    $user = $dom->createElement("Users");
    $root = $dom->appendChild($user);

    $sxe = simplexml_import_dom($dom);

    $user1 = $sxe->addChild('user');
    $user1->addChild("firstname", "Billy");
    $user1->addChild("lastname", "Anyteen");
    $user1->addAttribute('role', 'student');

    $user2 = $sxe->addChild('user');
    $user2->addChild('firstname', 'Debbie');
    $user2->addChild('lastname', 'Little');
    $user2->addAttribute('role', 'teacher');

    $xmlString = $sxe->saveXML();
    echo $xmlString;

}catch (Exception $d){
    echo $e->getMessage();
}

When I look at the output in either my browser's developer tools network tab or via command line php the output comes out inline:

Chrome Dev tools:

<?xml version="1.0"?>
<Users><user role="student"><firstname>Billy</firstname><lastname>Anyteen</lastname></user><user role="teacher"><firstname>Debbie</firstname><lastname>Little</lastname></user></Users>

Command line:

$ php index.php
<?xml version="1.0"?>
<Users><user role="student"><firstname>Billy</firstname><lastname>Anyteen</lastname></user><user role="teacher"><firstname>Debbie</firstname><lastname>Little</lastname></user></Users>

Is this the expected result? From the property description I would expect the output to look like this:

<?xml version="1.0"?>
<Users>
    <user role="student">
        <firstname>Billy</firstname>
        <lastname>Anyteen</lastname>
    </user>
    <user role="teacher">
        <firstname>Debbie</firstname>
        <lastname>Little</lastname>
    </user>
</Users>

If the formatOutput property isn't the way I should be getting this kind of spacing and indenting, is there a way that I can get the output in that fashion? (I've seen the tag mentioned but it seems to be a depreciated tag).

Chris Schmitz
  • 20,160
  • 30
  • 81
  • 137
  • See the accepted answer of the duplicate question, you're missing setting `$doc->preserveWhiteSpace = false;` probably. If that Q&A doesn't help, please edit your question giving reference and ping me here in comments. – hakre Oct 26 '14 at 16:08
  • I added the `$dom->preserveWhiteSpace = false;` along with setting the version of `domDocument` on instantiation. To take it a step further I tried using `saveXML()` to convert it to a string and then echoed the string itself and I'm still getting the same results. I haven't tried the tidy function that the answer mentioned yet because I wanted to make sure I wasn't messing something up in what I already had. – Chris Schmitz Oct 26 '14 at 17:15
  • @hakre the supposed duplicate question is about DOMDocument. The OP here switches to using SimpleXML element half way through the code. – i alarmed alien Oct 26 '14 at 18:05
  • @ialarmedalien, ah, so the format doesn't cary through when using SimpleXML? That's my mistake, I thought it would retain it's formatting. – Chris Schmitz Oct 26 '14 at 18:06
  • @ChrisSchmitz Your problem is that you've suddenly switched over to using simpleXML (`$sxe = simplexml_import_dom($dom);`), but the formatOutput command is for DOMDocument. I would suggest you convert the `$sxe->addChild` and `$sxe->addAttribute` to their DOMDocument equivalents (or run `$dom->loadXML( $xmlString );`) , and then you'll get beautifully formatted XML! – i alarmed alien Oct 26 '14 at 18:07
  • You can even use both intermixed (via `simplexml_import_dom`) they both share the same document in memory then (both libraries are sisters). But to profit from the output formatting you need to use the **DOMDocument** for outputting as **SimpleXMLElement** has a limited feature set. /cc @ialarmedalien – hakre Oct 26 '14 at 22:03

0 Answers0