devdavad Posted February 11, 2009 Share Posted February 11, 2009 This is a bit of a frustration: I would like an output like this: <?xml version="1.0"?> <parent> <one>aseot</one> <two>baotoeu</two> </parent> so I would write a script like this to try to achieve that <?php $dom = new DOMDocument(); $dom->preserveWhiteSpace = true; $theParent = $dom->createElement("parent"); $newElement = $dom->createElement("one", "aseot"); $newOtherElement = $dom->createElement("two", "baotoeu"); $theParent->appendChild($newElement); $theParent->appendChild($newOtherElement); $nodeTheParent = $dom->appendChild($theParent); header("Content-Type: text/plain"); echo $dom->SaveXML(); ?> but unfortunately this is outputted <?xml version="1.0"?> <parent><one>aseot</one><two>baotoeu</two></parent> where everything is on the same line. So my question is: is it possible to hack the DOM class in order to achieve new lines after the end of every element? Or: is there a physical DOM.class.php file that can be extended to get the new lines after the end of every element? Quote Link to comment Share on other sites More sharing options...
devdavad Posted February 12, 2009 Author Share Posted February 12, 2009 here's kind of an ugly work around that I found: <?php $dom = new DOMDocument(); $theParent = $dom->createElement("parent"); $newElement = $dom->createElement("one"); $newElementText = $dom->createTextNode("\naseuth\n"); $newOtherElement = $dom->createElement("two"); $newOtherElementText = $dom->createTextNode("\nstgiku\n"); $newElement->appendChild($newElementText); $newOtherElement->appendChild($newOtherElementText); $theParent->appendChild($newElement); $theParent->appendChild($newOtherElement); $dom->appendChild($theParent); $nodeTheParent = $dom->appendChild($theParent); header("Content-Type: text/plain"); echo $dom->SaveXML(); ?> Quote Link to comment Share on other sites More sharing options...
DarkWater Posted February 17, 2009 Share Posted February 17, 2009 Change the Content-Type to text/xml. Quote Link to comment Share on other sites More sharing options...
semlabs Posted May 6, 2009 Share Posted May 6, 2009 You do it like this: $doc = new DOMDocument(); $doc->load( 'doc.xml' ); $doc->formatOutput = true; $doc->preserveWhiteSpace = false; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.