Discussion:
Some help with SimpleXML :`(
Matthew Croud
2009-09-03 15:03:58 UTC
Permalink
Hi foks,

Could someone lend me a little hand with simpleXML ?
I've been looking at examples in the PHP manual and W3C, i'm at the
pinnacle of figuring it out but alas i need a hand from the mailing
list dudes.

My aim is to add another "item" node, so i'll start with my XML file
called "items.xml"

Item.xml_____________________________

<?xml version="1.0" encoding="iso-8859-1"?>
<clothes>

<item>
<name>Red Gloves</name>
<desc>a pair of red gloves</desc>
<size>adult</size>
<price>12.99</price>
</item>

<item>
<name>Snow Boots</name>
<desc>some snow boots</desc>
<size>child</size>
<price>23.99</price>
</item>

</clothes>
______________________________________


And now the PHP i'm using to create a new "item" node:


$xml = simplexml_load_file('items.xml');

$item = $xml->clothes->addChild('item');
$item->addChild('name', 'blue gloves');
$item->addChild('desc', 'some blue gloves');
$item->addChild('size', 'adult');
$item->addChild('price', '4.99');

______________________________________

The error i'm getting is: "Cannot add child. Parent is not a
permanent member of the XML tree"


Help me Gamesmaster!
Matt
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
J DeBord
2009-09-03 17:02:53 UTC
Permalink
You almost had it...

Since clothes is the root element you can think of your initial $xml as
<clothes>. So, you add a child to it. That error message is kinda cryptic
though.

<?php

$xml = <<<EOD
<?xml version="1.0" encoding="iso-8859-1"?>
<clothes>

<item>
<name>Red Gloves</name>
<desc>a pair of red gloves</desc>
<size>adult</size>
<price>12.99</price>
</item>

<item>
<name>Snow Boots</name>
<desc>some snow boots</desc>
<size>child</size>
<price>23.99</price>
</item>

</clothes>
EOD;

$sxml = simplexml_load_string($xml);

$item = $sxml->addChild('item');
$item->addChild('name', 'Blue Gloves');

echo $sxml->asXML();

exit;
Post by Matthew Croud
Hi foks,
Could someone lend me a little hand with simpleXML ?
I've been looking at examples in the PHP manual and W3C, i'm at the
pinnacle of figuring it out but alas i need a hand from the mailing list
dudes.
My aim is to add another "item" node, so i'll start with my XML file called
"items.xml"
Item.xml_____________________________
<?xml version="1.0" encoding="iso-8859-1"?>
<clothes>
<item>
<name>Red Gloves</name>
<desc>a pair of red gloves</desc>
<size>adult</size>
<price>12.99</price>
</item>
<item>
<name>Snow Boots</name>
<desc>some snow boots</desc>
<size>child</size>
<price>23.99</price>
</item>
</clothes>
______________________________________
$xml = simplexml_load_file('items.xml');
$item = $xml->clothes->addChild('item');
$item->addChild('name', 'blue gloves');
$item->addChild('desc', 'some blue gloves');
$item->addChild('size', 'adult');
$item->addChild('price', '4.99');
______________________________________
The error i'm getting is: "Cannot add child. Parent is not a permanent
member of the XML tree"
Help me Gamesmaster!
Matt
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Matthew Croud
2009-09-04 08:23:45 UTC
Permalink
Well, you guys are awesome.

So the script below doesn't cause any errors (nice), however it
doesn't save the newly added child to the xml file (items.xml):



$xml = simplexml_load_file("items.xml");

$item = $xml->addChild('item');
$item->addChild('name', $name);
$item->addChild('desc', $desc);
$item->addChild('size', $size);
$item->addChild('price', $price);



I thought it would ? would i need to "write" using fwrite ?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
J DeBord
2009-09-04 09:31:02 UTC
Permalink
Post by Matthew Croud
Well, you guys are awesome.
So the script below doesn't cause any errors (nice), however it doesn't
$xml = simplexml_load_file("items.xml");
$item = $xml->addChild('item');
$item->addChild('name', $name);
$item->addChild('desc', $desc);
$item->addChild('size', $size);
$item->addChild('price', $price);
I thought it would ? would i need to "write" using fwrite ?
You'll need to save your xml to the file once you are done manipulating it.
Use $xml->asXML('filename'); Check here:
http://www.php.net/manual/en/simplexmlelement.asXML.php

J
Post by Matthew Croud
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Continue reading on narkive:
Loading...