1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Development PHP/XML: new DOM Nodes inside rss Nodes don't format properly

Discussion in 'Software' started by OneSeventeen, 6 Feb 2011.

  1. OneSeventeen

    OneSeventeen Oooh Shiny!

    Joined:
    3 Apr 2002
    Posts:
    3,454
    Likes Received:
    2
    If I use the PHP DOM object to append a new child before or after my rss element it formats perfectly.

    If I instead put the new DOM Node inside the rss tag (as though I wanted to add it as my most recent item... go figure) then it loses all formating and stays on one line...

    Example of it inserted inside RSS tag:
    Code:
    <?xml version="1.0" encoding="UTF-8"?> 
    <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0"> 
    <item><title>some title</title><author>OneSeventeen</author></item>
    </rss>
     
    Example of the same thing inserted outside the RSS tag:
    Code:
    <?xml version="1.0" encoding="UTF-8"?> 
    <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0"> 
    </rss>
    <item>
        <title>some title</title>
        <author>OneSeventeen</author>
    </item>
    </item>
     
    BIG UPDATE:
    I added a tag around the rss tag, and had the same poor formatting problem! (even with the new node inserted outside the rss tag but inside my new "foo" tag)

    New theory: adding a node inside an XML item loaded from a file makes formatting teh suck by default.

    now if I pass LIBXML_NOBLANKS as the $options argument of DOMDocument::load($file, $options) then it formats the whole document (including my new stuff) properly.
     
    Last edited: 6 Feb 2011
  2. OneSeventeen

    OneSeventeen Oooh Shiny!

    Joined:
    3 Apr 2002
    Posts:
    3,454
    Likes Received:
    2
    Also, I read an awesome tip on the PHP manual for formatOutput:
    PHP:
    $xml = new DOMDocument::load('somefile.xml');
    $xml->formatOutput true;
    //do a bunch of manipulation

    //now pass the supposedly (but not really) formatted output to a variable:
    $outXML $xml->saveXML(); //I'm a crappily formated XML feed, boo hoo

    //now create a brand new XML document
    $xml = new DOMDocument(); 
    $xml->preserveWhiteSpace false
    $xml->formatOutput true//yup, going to try to make this format again
    //pass the output of the first bunch of stuff we did to the new XML document:
    $xml->loadXML($outXML); 
    //now display the cleanly formatted output without using LIBXML_NOBLANKS (which may have undesired consequences
    print $xml->saveXML(); 
    original comment

    Wish I had seen that comment earlier!
     

Share This Page