Results 1 to 1 of 1

Thread: xml addChild but what about it's sister?!

  1. #1
    Join Date
    Jan 2008
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default xml addChild but what about it's sister?!

    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:

    Code:
    <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.
    PHP Code:
    $xml = new SimpleXMLElement('new.xml'nulltrue);

    $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!
    Last edited by rpflorence; 02-11-2008 at 03:01 AM. Reason: changed title

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •