Page 1 of 1

Appending XML php5 possible tutorial?

Posted: Mon Jan 29, 2007 1:06 pm
by mavedog21
Hello,

curious if anyone knows a good place to learn how to append my $dom object to
an already exisiting file:

psuedo code:

Code: Select all

<?php

   $dom = new DomDocument;
   $dom->load('myXML.xml');

##### Can Do This ###########

  // create my xml elements and nodes
  // append new element and nodes to $dom

#########################


#### HARD PART ########

  // add new $dom to xml without overwriting old xml elements

####################

    $dom->save('myXML.xml')
  // save over original file.

?>
So original xml file would start out like:

Code: Select all

<?xml .....

    <people>
         <person>
            <name> Tom</name>
         </person>
    </people>
and end up after adding the new $dom info

Code: Select all

<?xml .....

    <people>
         <person>
            <name> Tom</name>
         </person>

         <person>
             <name> Jane </name>
         </person>
    </people>

I can add to my root as a new root element but I want to add my $dom into my <people> element.
Is there a tutorial on how to do this. I can't find one out there (yet). I'd like to read that before posting questions (besides this one).

thanks,

sky :D

Posted: Mon Jan 29, 2007 1:16 pm
by Ollie Saunders

Posted: Mon Jan 29, 2007 2:24 pm
by Kieran Huggins
...and don't forget to create a single root element in your XML!

Posted: Tue Jan 30, 2007 5:35 pm
by mavedog21
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

Code: Select all

$dom->createRootNode

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

Code: Select all

$dom->formatoutput = true;
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

Code: Select all

$dom->preserveWhiteSpace = false;
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>

Posted: Tue Jan 30, 2007 5:47 pm
by Ollie Saunders
Possibly

Code: Select all

$dom->appendChild($dom, 'thexml.xml');
Should be

Code: Select all

$theXml = new DOMDocument();
$theXml->load('thexml.xml');
$rootNode->appendChild($theXml);

Posted: Wed Jan 31, 2007 5:09 am
by volka
Why did you replace
mavedog21 wrote:$dom->save('myXML.xml')
by
mavedog21 wrote:########## Saves new $dom object with appended input information #########

$dom->appendChild($dom, 'thexml.xml');
?

Posted: Wed Jan 31, 2007 10:14 am
by mavedog21
sorry just confused.

I just can't seem to get my $dom to append into my xml files root. The ->appendChild in that
was suppose to be a ->save().

still can't get my $rootNode to append inside my $dom's rootNode?
(laugh) why is this so confusing for me?

Posted: Wed Jan 31, 2007 10:49 am
by volka
try

Code: Select all

<?php
$xml = <<< eox
<people>
     <person>
        <name>Marcus</name>
    </person>
</people>
eox;


$text = /* $_POST["name"] */ 'yadda yadda';

$dom = new DomDocument;
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;

$dom->loadxml($xml);

$person=$dom->createElement('person');
$name=$dom->createElement('name', $text);

$person->appendChild($name);
$dom->documentElement->appendChild($person);
 
echo $dom->saveXML();
?>
createElement can take two parameters, the second beeing the new contents of the element.
You want to add a new element to the document's root element which is $dom->documentElement

Posted: Wed Jan 31, 2007 11:00 am
by mavedog21
thank you. I'll try this out.

that's a lot of good info. I think my biggest problem was I was missing the:
You want to add a new element to the document's root element which is $dom->documentElement

Posted: Fri Feb 02, 2007 1:44 pm
by mavedog21
THIS WORKS AWESOME!

Thank you I've been able to do exactly what I want to do. Though not without the help from you guys.

Thanks A Bunch.... I'm So Stinkin' EXCITED!
Is there a way to mark this topic Answered?