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

Link to comment
Share on other sites

  • 2 months later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.