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
Forgot to mention you also need to have the line $xdoc->preserveWhiteSpace = false; PHP: just after the new domdocument line
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.