Link to home
Start Free TrialLog in
Avatar of duder78

asked on

Adding elements inside root node using DOM with PHP


I'm putting together a little web document that can update an XML file based on user input.  I can read the contents...that was the easy part.  Now I want to add elements to the file.  First, here's the structure of my XML file :

<?xml version="1.0" encoding="utf-8"?>
<advertisements date="03/03/2011">
	<item name="hamburger">
		<metric>6 patties</metric>
		<description>Pellentesque sodales commodo ligula, id blandit urna semper id. Integer faucibus ligula ac risus laoreet ullamcorper. Nam ut elit velit. Sed in ante ante. Suspendisse enim eros, facilisis a gravida ut, sollicitudin eget vetit.</description>
	<item name="jogging pants">
		<metric>1 pair</metric>
		<description>This is a fantastic pair of jogging pants from Giant Tiger.  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut bibendum, nunc a auctor adipiscing, eros urna imperdiet purus, ut bibendum tellus lacus sed tellus. Ut ipsum augue, dapibus vel placerat in, malesuada vitae nunc. Suspendisse semper placerat elit.</description>

Open in new window

All I want to do is add new a new item inside of the root node using the DOM.  In my code, I have taken the steps to create the new elements but can't figure out how to add them inside of the root <advertisements> node.  I'm sure it's ridiculously easy, but i've googled it at length and haven't stumbled upon anything yet.  Any help would be much appreciated.

Here's my PHP file :

$items = array();
  $items [] = array(
  'image' => 'dishes',
  'price' => 20.99,
  'metric' => 5,
  'description' => 'Maecenas risus massa, pulvinar at rhoncus vel, faucibus ut orci. Ut at tortor ornare neque convallis porttitor. Donec feugiat sem in leo feugiat imperdiet. Praesent vel pulvinar ante. Nulla eu diam erat, pellentesque vehicula elit. Aenean at erat mauris.'
  $doc = new DOMDocument();
  $doc->formatOutput = true;
  $doc->load( 'XML/gt.xml' );
  $root = $doc->getElementsByTagName("advertisements");

  foreach( $items as $item )  {
  $new_item = $doc->createElement( "item" );
  $image = $doc->createElement( "image" );
  $doc->createTextNode( $item['image'] )
  $new_item->appendChild( $image );
  $price = $doc->createElement( "price" );
  $doc->createTextNode( $item['price'] )
  $new_item->appendChild( $price );
  $metric = $doc->createElement( "metric" );
  $doc->createTextNode( $item['metric'] )
  $new_item->appendChild( $metric );
  $description = $doc->createElement( "description" );
  $doc->createTextNode( $item['description'] )
  $new_item->appendChild( $description );
  $root->appendChild( $new_item );
  echo $doc->saveXML();

Open in new window

I'm getting a 'Fatal error: Call to undefined method DOMNodeList::appendChild() in C:\xampp\htdocs\XML\index2.php on line 46' error message.  I don't think it likes the line where I grab the root element using getElementsByTagName.  Thank you in advance...

Avatar of merwetta1

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of duder78


Perfect, simple as pie.  I guess i'll use simpleXML instead then.  Works like a champ, thank you very much.