Jump to content

want to do some hacking with the DOM


devdavad

Recommended Posts

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

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();
?>

  • 2 months later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.