karl-phpfreaks Posted November 29, 2011 Share Posted November 29, 2011 I have this code reads the xml runs with no errors but will not input the data from the array in the <catparent> The script <?php // isbn => pages $page_numbers = array( '1234' => 'hello parent', // insert data into catparent node '5678' => 'hello parent', // insert data into catparent node ); $dom = new DOMDocument(); $dom->load('edtest1.xml'); $xpath = new DOMXPath($dom); $items = $xpath->query('item'); foreach($items as $item) { $catcode = $item->getElementsByTagName['catcode']; $parent_code = $page_numbers[ $catcode[0] ]; $item->appendChild( $dom->createElement('catparent', $parent_code) ); } $dom->save('edtest_stack.xml'); ?> My XML file looks like <?xml version="1.0" encoding="UTF-8"?> <items> <item> <catcode>1234</catcode> <catdesc>Systems - System Bundles</catdesc> </item> <item> <catcode>5678</catcode> <catdesc>Hard Drives - SATA</catdesc> </item> </items> Output XML looks like this as you can see it has created the <catparent> but will not insert the data from the array. <?xml version="1.0" encoding="UTF-8"?> <items> <item> <catcode>1234</catcode> <catdesc>Systems - System Bundles</catdesc> <catparent></catparent></item> <item> <catcode>5678</catcode> <catdesc>Hard Drives - SATA</catdesc> <catparent></catparent></item> </items> Any ideas pointers as to where I'm going wrong would be most helpful Quote Link to comment https://forums.phpfreaks.com/topic/252061-php-find-replace-xml-data/ Share on other sites More sharing options...
xyph Posted November 29, 2011 Share Posted November 29, 2011 SimpleXML is an easier XML solution than DOM, IMO. <?php $xml_data = <<<HEREDOC <?xml version="1.0" encoding="UTF-8"?> <items> <item> <catcode>1234</catcode> <catdesc>Systems - System Bundles</catdesc> </item> <item> <catcode>5678</catcode> <catdesc>Hard Drives - SATA</catdesc> </item> </items> HEREDOC; $page_numbers = array( '1234' => 'hello parent1', '5678' => 'hello parent2', ); $xml = new SimpleXMLElement( $xml_data ); // Loop through all items in XML foreach( $xml->item as $item ) { // Check if 'catcode' is a child of this item // and if $page_numbers has a matching key // In order to use the value as an array key, we need to explicitly // call it as a string, using typecasting (string) if( isset($item->catcode) && isset($page_numbers[(string)$item->catcode]) ) // Create a new child under this item, using the $page_numbers value $item->addChild( 'catparent', $page_numbers[(string)$item->catcode] ); } // Output the XML echo $xml->saveXML(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/252061-php-find-replace-xml-data/#findComment-1292328 Share on other sites More sharing options...
karl-phpfreaks Posted November 29, 2011 Author Share Posted November 29, 2011 You have made a frustrated man very happy. So to output to an xml what would I need at the end please. But thanks very much again Quote Link to comment https://forums.phpfreaks.com/topic/252061-php-find-replace-xml-data/#findComment-1292343 Share on other sites More sharing options...
xyph Posted November 29, 2011 Share Posted November 29, 2011 The manual will help you with that http://php.net/manual/en/simplexmlelement.asxml.php Quote Link to comment https://forums.phpfreaks.com/topic/252061-php-find-replace-xml-data/#findComment-1292345 Share on other sites More sharing options...
karl-phpfreaks Posted November 29, 2011 Author Share Posted November 29, 2011 Yep the manual helped with that thanks. So could you point me at how to use an xml file rather than the heredoc statement Quote Link to comment https://forums.phpfreaks.com/topic/252061-php-find-replace-xml-data/#findComment-1292369 Share on other sites More sharing options...
karl-phpfreaks Posted November 29, 2011 Author Share Posted November 29, 2011 I used this code to sort the problem it's not the best formed I don't think but it works. Now I need to cure case sensitive issues <?php $xml = simplexml_load_file('edtest1.xml'); $page_numbers = array( 'SWOT' => 'Software', 'SWUT' => 'Software', 'VOGA' => 'VOIP', ); // Loop through all items in XML foreach( $xml->item as $item ) { // Check if 'catcode' is a child of this item // and if $page_numbers has a matching key // In order to use the value as an array key, we need to explicitly // call it as a string, using typecasting (string) if( isset($item->catcode) && isset($page_numbers[(string)$item->catcode]) ) // Create a new child under this item, using the $page_numbers value $item->addChild( 'catparent', $page_numbers[(string)$item->catcode] ); } // Output the XML echo $xml->saveXML(test.xml); ?> Quote Link to comment https://forums.phpfreaks.com/topic/252061-php-find-replace-xml-data/#findComment-1292386 Share on other sites More sharing options...
xyph Posted November 30, 2011 Share Posted November 30, 2011 If the keys of $page_numbers is always uppercase, You could simply use strtoupper to make the $item->catcode UPPERCASE. Otherwise, I would first loop through $page_numbers and change all of the keys to lowercase (preference) and use strtolower on $item->catcode. Quote Link to comment https://forums.phpfreaks.com/topic/252061-php-find-replace-xml-data/#findComment-1292521 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.