Some help with SimpleXML :`(

Some help with SimpleXML :`(

am 03.09.2009 17:03:58 von 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_____________________________





Red Gloves
a pair of red gloves
adult
12.99



Snow Boots
some snow boots
child
23.99



______________________________________


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

Re: Some help with SimpleXML :`(

am 03.09.2009 19:02:53 von J DeBord

--0014852f5fa4dfc4ec0472af5be3
Content-Type: text/plain; charset=UTF-8

You almost had it...

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


$xml = <<



Red Gloves
a pair of red gloves
adult
12.99



Snow Boots
some snow boots
child
23.99



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 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_____________________________
>
>
>
>
>
> Red Gloves
> a pair of red gloves
> adult
> 12.99
>

>
>
> Snow Boots
> some snow boots
> child
> 23.99
>

>
>

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

--0014852f5fa4dfc4ec0472af5be3--

Re: Some help with SimpleXML :`(

am 04.09.2009 10:23:45 von Matthew Croud

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

Some help with SimpleXML :`(

am 04.09.2009 11:31:02 von J DeBord

--00c09ffb5975c674820472bd2937
Content-Type: text/plain; charset=UTF-8

On Fri, Sep 4, 2009 at 10:23 AM, Matthew Croud wrote:

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

>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--00c09ffb5975c674820472bd2937--