karl-phpfreaks Posted November 28, 2011 Share Posted November 28, 2011 From this tutorial - section on Adding nodes http://www.phpfreaks.com/tutorial/handling-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. Script <?php // isbn => pages $page_numbers = array( '1234' => '654', // A Thousand Splendid Suns '5678' => '789', // The Brief Wondrous Life of Oscar Wao ); $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'); ?> The 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> XML output 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> <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. 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 an example or just highlight the correct needed. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/251941-read-xml-in-php/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.