Log in

View Full Version : xml addChild but what about it's sister?!



rpflorence
02-11-2008, 03:00 AM
Still very new to PHP but learning very quickly thanks to a few tips here and google.

I'm trying to tackle xml. I have figured out how to add information using addChild but ran into a snag.

Say for example I'm trying to create this into an xml file:



<movies>
<movie>
<title>Stardust</title>
<characters>
<character>
<name>Tristan</name>
<actor>Charlie Cox</name>
</character>
<character>
<name>Yvaine</name>
<actor>Claire Danes</actor>
</character>
</characters>
</movie>
</movies>


I can create that whole thing except for under characters I can't figure out how to add more than one. I could create it with this code and get just Tristan played by Charlie Cox, but I don't even know how to approach getting a second character in (all of my attempts either fail miserably or just add another name and actor to the first character rather than creating a unique "sister").

Here's my code to get one character in.


$xml = new SimpleXMLElement('new.xml', null, true);

$movie = $xml->addChild('movie');
$movie->addChild('title','Stardust');
$movie->addChild('characters');
$movie->characters->addChild('character');
$movie->characters->character->addChild('name','Tristan');
$movie->characters->character->addChild('actor','Charlie Cox');


Thanks!