Jump to content

Read XML in php


karl-phpfreaks

Recommended Posts

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.  :shrug: 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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.