Adding new Element into XML doc

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
neophp
Forum Newbie
Posts: 1
Joined: Tue Sep 22, 2009 10:31 am

Adding new Element into XML doc

Post by neophp »

Hi

This is my first post on this board...

I am doing some work with XML..... I need to add some elements INTO the document and i am struggling

Here is an example of what the XML looks like (note: this is one part of the full doc)

Code: Select all

 
<product>
<id>99</id>
<name>XXXXX</name>
<rate>
<id>55</id>
<price>55.00</price>
</rate>
<rate>
<id>44</id>
<price>333.00</price>
</rate>
</product>
 
 
the code loops around and outputs the "rate" nodes and its values onto the price page, I now need it to add X number of blank "rate" nodes to the doc so the code will output empty stuff , the whys and wheres etc are not important.

I tried the below:

Code: Select all

 
 $prod_xml = $prod->asXML();
      $dom = new DOMDocument();
      $dom->loadXML($prod->asXML());
      $element = $dom->createElement("rate");
      $dom->appendChild($element);
      $prod_new =   $dom->saveXML();
 
 
However I got the following

Code: Select all

 
.......
</product>
<rate></rate>
 
I need the empty rate to be inside the product node , and I am unsure how to do it ....

I hope this makes sense ...

Moh
Post Reply