That is my problem... creating the rootNode.
When I save my new append info. it appends it after the rootNode.
How do I encompass my new appended info. into my $doms rootNode?
when I save my $dom into my XML file my XML looks like this:
Code: Select all
<rootNode>
<firstChild> First Child </firstChild>
<childNode> First Childs, First Child </childNode>
</firstChild>
</rootNode>
// already exists in xml file
<secondChild> Second Child </secondChild>
<childNode> Second Childs, First Child </childNode>
</secondChild>
// append info that I want in my <rootNode>.
How can I add it inside my rootNode?
I didn't find anything in the manual for php5
Here is the code I'm currently using:
Code: Select all
<?php
$dom = new DomDocument;
$dom->preserveWhiteSpace = false;
$dom->load('thexml.xml');
$dom->formatOutput = true;
############ Create Elements and Nodes ########################
$rootElement=$dom->createElement('person');
$nameNode=$dom->createElement('name');
$textNode=$dom->createTextNode($_POST["name"]);
############# Append Nodes to $dom object #####################
$nameNode->appendChild($textNode);
$rootElement->appendChild($nameNode);
$dom->appendChild($rootElement);
############## Posts response into html page ###################
echo "You used \"newxmlfile\". Thank you ".$_POST["name"]." for registering.";
########## Saves new $dom object with appended input information #########
$dom->appendChild($dom, 'thexml.xml');
?>
and my XML result is (cleaned up by hand):
Code: Select all
<?xml version="1.0"?>
<people>
<person>
<name>Marcus</name>
</person>
</people>
####### Added with html input form########
<person>
<name>Martin</name>
</person>
// needs to be added into <people> node
It can't be something to hard?
And is there an obvious reason why my
does not output an easy to read file? I read it's suppose to but won't for me. In the
manual one person commented that
needs to preceed the $dom creation???
It took some searching to figure this one out. I didn't see much in the way of explaining this glitch in the manual thus far. (For PHP5 I believe)
formatOutput = true; appears to fail when the origin of the DOM came from a file via load().
Will not indent the output and will display the modified nodes all in one long line. Makes for editing a config.xml a bit difficult when saving to a file.
By adding the preserveWhiteSpace = false; BEFORE the load() the formatOutput works as expected.
found here:
http://us2.php.net/manual/en/function.d ... avexml.php
please help I'm confused. it doesn't format my xml file it always looks like this:
Code: Select all
<?xml version="1.0"?><people><person><name>Marcus</name></person></people><person><name>Martin</name></person>