Jump to content

Read Xml In Php


karl-w3schoolsforum

Recommended Posts

From this tutorial - section on Adding nodes over at PHPFreaks, I did post over there but have had no replies strange as the tutorial was written by them. http://www.phpfreaks...ndling-xml-data When I use my xml file it creates the node but does not insert the new data. There are no page errors. I'm sure I have just missed something very simple and after hours of trying I will now bow down and ask for help. This is the script I'm using

<?php// isbn => pages$page_numbers = array(				'1234' => '654', // insert data into catparent node				'5678' => '789', // insert data into catparent node		);$dom = new DOMDocument();$dom->load('edtest.xml');$xpath = new DOMXPath($dom);$items = $xpath->query('item');foreach($items as $item){		$item->appendChild($dom->createElement('catparent', $page_numbers[$item->getAttribute('catcode')]));}$dom->save('edtest_new.xml');?>

My XML file

<?xml version="1.0" encoding="UTF-8"?><items><item><catcode>1234</catcode><catdesc>Systems - System Bundles</catdesc><price_cost>999.95</price_cost><price_sell>999.95</price_sell></item></items>

Output XML

<?xml version="1.0" encoding="UTF-8"?><items><item><catcode>1234</catcode><catdesc>Systems - System Bundles</catdesc><price_cost>999.95</price_cost><price_sell>999.95</price_sell> <catparent></catparent> // it creates but does not insert required data </item></items>

It runs the script creates the required node, but it does not insert the required data. shrug.gif The object of the script is to find <catcode> = 1234 and add a new <catparent> </catparent> with required data from the array. If there is a better way to achieve the result or just the correction needed. Thanks

Link to comment
Share on other sites

Check that $item->getAttribute('catcode') has the value you expect (using echo), then check that $page_numbers[$item->getAttribute('catcode')] has the value you expect.

Link to comment
Share on other sites

I told you to look to see if the program is getting the values you expected. Print out the values of $item->getAttribute('catcode') and then $page_numbers[$item->getAttribute('catcode')] to see what's in them.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...