Append Child to End of Specific Spot on XML using PHP

Ruickucturoke

New Member
I am trying to append data to the end of a specific place in the XML Below.My XML is a lot longer, but for instance, I want to append Data to the end of the section labeled "Delete". I would then be adding different data to the end of "Total Tickets", but if I can somehow figure out how to append data to the end of the first child, I can figure out the rest.\[code\]<report><sets><set> <legend>Delete</legend> <values> <value date="2012-06-24" data="http://stackoverflow.com/questions/11490854/9"/> <value date="2012-06-25" data="http://stackoverflow.com/questions/11490854/17"/> <value date="2012-06-26" data="http://stackoverflow.com/questions/11490854/15"/> <value date="2012-06-27" data="http://stackoverflow.com/questions/11490854/10"/> <value date="2012-06-28" data="http://stackoverflow.com/questions/11490854/8"/> </values></set> <set> <legend>Total Tickets</legend> <values> <value date="2012-06-24" data="http://stackoverflow.com/questions/11490854/412"/> <value date="2012-06-25" data="http://stackoverflow.com/questions/11490854/416"/> <value date="2012-06-26" data="http://stackoverflow.com/questions/11490854/423"/> <value date="2012-06-27" data="http://stackoverflow.com/questions/11490854/405"/> <value date="2012-06-28" data="http://stackoverflow.com/questions/11490854/280"/> </values></set></sets></report>\[/code\]Below is a test script I wrote to see the results. In this instance, I am trying to append the Element 'value' with Attribute data of '12345' at the end of the list of the legend "Delete"\[code\]<?php$doc = new DOMDocument;$doc->load('result.xml');$test = $doc->getElementsByTagName("legend")->item(0);echo $test->nodeValue.PHP_EOL; //should print Deleteif("Delete" == $test->nodeValue){ $newElement = $doc->createElement('value'); $theAttribute = $doc->createAttribute('date'); $theAttribute->value='http://stackoverflow.com/questions/11490854/12345'; $newElement->appendChild($theAttribute); $doc->appendChild($newElement);}echo $doc->saveXML();?>\[/code\]The Results I get is this:\[code\]<report><sets><set> <legend>Delete</legend> <values> <value date="2012-06-24" data="http://stackoverflow.com/questions/11490854/9"/> <value date="2012-06-25" data="http://stackoverflow.com/questions/11490854/17"/> <value date="2012-06-26" data="http://stackoverflow.com/questions/11490854/15"/> <value date="2012-06-27" data="http://stackoverflow.com/questions/11490854/10"/> <value date="2012-06-28" data="http://stackoverflow.com/questions/11490854/8"/> </values></set> <set> <legend>Total Tickets</legend> <values> <value date="2012-06-24" data="http://stackoverflow.com/questions/11490854/412"/> <value date="2012-06-25" data="http://stackoverflow.com/questions/11490854/416"/> <value date="2012-06-26" data="http://stackoverflow.com/questions/11490854/423"/> <value date="2012-06-27" data="http://stackoverflow.com/questions/11490854/405"/> <value date="2012-06-28" data="http://stackoverflow.com/questions/11490854/280"/> </values></set></sets></report><value date="12345"/>\[/code\]I'm trying to get the data shown as this instead:\[code\]<report><sets><set> <legend>Delete</legend> <values> <value date="2012-06-24" data="http://stackoverflow.com/questions/11490854/9"/> <value date="2012-06-25" data="http://stackoverflow.com/questions/11490854/17"/> <value date="2012-06-26" data="http://stackoverflow.com/questions/11490854/15"/> <value date="2012-06-27" data="http://stackoverflow.com/questions/11490854/10"/> <value date="2012-06-28" data="http://stackoverflow.com/questions/11490854/8"/> <value date="12345"/> </values></set>\[/code\]Not sure how I can get the data to be added to that specific spot instead of the very end of the XML.I appreciate anyones help with this.
 
Back
Top