Jump to content

karl-phpfreaks

New Members
  • Posts

    8
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

karl-phpfreaks's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. 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); ?>
  2. Yep the manual helped with that thanks. So could you point me at how to use an xml file rather than the heredoc statement
  3. 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
  4. 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
  5. 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
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.