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;


On Thu, Sep 3, 2009 at 5:03 PM, Matthew Croud <m...@obviousdigital.com>wrote:

> 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
>
>

Reply via email to