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? Link to comment https://forums.phpfreaks.com/topic/144709-want-to-do-some-hacking-with-the-dom/ 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(); ?> Link to comment https://forums.phpfreaks.com/topic/144709-want-to-do-some-hacking-with-the-dom/#findComment-760133 Share on other sites More sharing options...
DarkWater Posted February 17, 2009 Share Posted February 17, 2009 Change the Content-Type to text/xml. Link to comment https://forums.phpfreaks.com/topic/144709-want-to-do-some-hacking-with-the-dom/#findComment-764660 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; Link to comment https://forums.phpfreaks.com/topic/144709-want-to-do-some-hacking-with-the-dom/#findComment-828003 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.