Help with DOM XML PHP...

Discussion in 'PHP' started by valiik, Feb 5, 2010.

  1. #1
    When I have the DOM write a new node to the XML page it adds it right after the node above and doesn't format it nicely (like we are used to seeing XML data formatted) like this:

    <pages>
    <page>value</page>
    <page>value2</page>
    <page>value3</page>
    </pages>

    instead it writes it like this:

    <pages><page>value</page><page>value2</page><page>value3</page></pages>>

    So what happens is when I try to replace that node it cant' find it... and if I go and fix the format just by dropping down the lines so they are formatted like this:

    it starts working just fine.

    So my question is, how do I get it to write nodes nicely, keeping the formatting?

    Here is the code I use:

    $xdoc = new DomDocument;
    $xdoc->Load('includes/content/pages.xml');
    			
    $candidate = $xdoc->getElementsByTagName('pages')->item(0);
    			
    $newPageElement = $xdoc ->createElement('page');
    			
    $newPermalinkElement = $xdoc ->createElement('permalink');
    $PermalinkNode = $xdoc ->createTextNode ($newpermalink);
    $newPermalinkElement -> appendChild($PermalinkNode);
    $newPageElement -> appendChild($newPermalinkElement);
    			
    $newNicenameElement = $xdoc ->createElement('nicename');
    $NicenameNode = $xdoc ->createTextNode ($newnicename);
    $newNicenameElement -> appendChild($NicenameNode);
    $newPageElement -> appendChild($newNicenameElement);
    			
    $newSEOtitleElement = $xdoc ->createElement('seotitle');
    $SEOtitleNode = $xdoc ->createTextNode ($newseotitle);
    $newSEOtitleElement -> appendChild($SEOtitleNode);
    $newPageElement -> appendChild($newSEOtitleElement);
    			
    $newMETAdescriptionElement = $xdoc->createElement('metadescription');
    $METAdescriptionNode = $xdoc ->createTextNode ($newmetadescription);
    $newMETAdescriptionElement -> appendChild($METAdescriptionNode);
    $newPageElement -> appendChild($newMETAdescriptionElement);
    			
    $newPagecontentElement = $xdoc ->createElement('pagecontent');
    $PagecontentNode = $xdoc ->createTextNode ($newpagecontent);
    $newPagecontentElement -> appendChild($PagecontentNode);
    $newPageElement -> appendChild($newPagecontentElement);
    			
    $candidate -> appendChild($newPageElement);
    			
    $test = $xdoc->save("includes/content/pages.xml");
    Code (markup):
    Any help will be appreciated.

    Thanks
     
    valiik, Feb 5, 2010 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    put
    $xdoc->formatOutput = true;
    PHP:
    before the save. That will do it hopefully
     
    JAY6390, Feb 5, 2010 IP
  3. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Forgot to mention you also need to have the line

    $xdoc->preserveWhiteSpace = false;
    PHP:
    just after the new domdocument line
     
    JAY6390, Feb 5, 2010 IP
    valiik likes this.
  4. valiik

    valiik Well-Known Member

    Messages:
    190
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #4
    Thanks JAY!

    I tried that and it didn't work but after doing some searching on formatOutput I found that you also need to add:

    $xdoc->preserveWhiteSpace = false;

    right before loading the XML document so here is what I did to the code at the top:

    $xdoc = new DomDocument();
    
    $xdoc->preserveWhiteSpace = false; // Added this
    			
    $xdoc->Load('includes/content/pages.xml');
    
    $xdoc->formatOutput = true; // AND added this
    Code (markup):
    You can read more on this here: http://php.net/manual/en/domdocument.savexml.php
    Do a page search for preserveWhiteSpace. It's almost at the bottom of the page from "devin at SPAMISBAD dot tritarget dot com".

    It is formatting it nicely now. Thanks!

    UPDATE: JAY, just saw your second post, thanks.
     
    Last edited: Feb 5, 2010
    valiik, Feb 5, 2010 IP
  5. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #5
    ^ read my post above ;)
     
    JAY6390, Feb 5, 2010 IP