SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XML DOM & nested elements

    I'm trying to nest multiple elements in my XML output using the PHP5 DOM.

    I want to get this, but can't get a nested element to work. Is what I'm trying to do illegal?
    Code:
    <items>
    	<books>
    		<writers>
    			<author>
    				<name></name>
    				<title></title>
    				<publisher></publisher>
    			</author>
    			<author>
    				<name></name>
    				<title></title>
    				<publisher></publisher>
    			</author>
    		</writers>
    	</books>
    </items>
    PHP Code:
    <?php
    $books 
    = array();
    $books [] = array(
    'title' => 'PHP Hacks',
    'name' => 'Jack Herrington',
    'publisher' => "O'Reilly"
    );
    $books [] = array(
    'title' => 'Podcasting Hacks',
    'name' => 'Jack Herrington',
    'publisher' => "O'Reilly"
    );

    $doc = new DOMDocument();
    $doc->formatOutput true;

    $r $doc->createElement("items");
    $doc->appendChild$r );

    foreach(
    $books as $book) {
      
    $b $doc->createElement"books" );
      
      
    // nested element
        
    $b $doc->createElement"writers" );
        
        
    $b $doc->createElement"author" );
      
      
    $name $doc->createElement"name" );
      
    $name->appendChild($doc->createTextNode$book['name'] ));
      
    $b->appendChild$name );
      
      
    $title $doc->createElement"title" );
      
    $title->appendChild($doc->createTextNode$book['title'] ));
      
    $b->appendChild$title );
      
      
    $publisher $doc->createElement"publisher" );
      
    $publisher->appendChild($doc->createTextNode$book['publisher'] ));
      
    $b->appendChild$publisher );
      
      
    $r->appendChild$b );
    }

    echo 
    $doc->saveXML();
    ?>

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,445
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    I tried your code and get
    Code XML:
    <?xml version="1.0"?>
    <items>
      <author>
        <name>Jack Herrington</name>
        <title>PHP Hacks</title>
        <publisher>O'Reilly</publisher>
      </author>
      <author>
        <name>Jack Herrington</name>
        <title>Podcasting Hacks</title>
        <publisher>O'Reilly</publisher>
      </author>
    </items>
    So "books" and "writers" are missing. Maybe they need to be appended?

  3. #3
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    So "books" and "writers" are missing. Maybe they need to be appended?
    That's the problem. How do I get those in there?

  4. #4
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Read your code like English, you have a grammatical error in your formatting.

    Your foreach loop basically reads "foreach author", yet you're trying to include <writers> & <books> inside of it, but should instead encapsulate it.

    You can append <books> to the end of <item>, and <writers> to the end of <books> and then run your loop to append <author> on the end of <writers>.

    Here's your code, modified to show you what you're doing wrong:

    PHP Code:
    <?php
    $books 
    = array();
    $books [] = array(
    'title' => 'PHP Hacks',
    'name' => 'Jack Herrington',
    'publisher' => "O'Reilly"
    );
    $books [] = array(
    'title' => 'Podcasting Hacks',
    'name' => 'Jack Herrington',
    'publisher' => "O'Reilly"
    );

    $doc = new DOMDocument();
    $doc->formatOutput true;

    $r $doc->createElement("items");
    $doc->appendChild$r );


    /** CHANGE */
    $foo $doc->createElement('foo');
    $r->appendChild($foo);


    foreach(
    $books as $book) {
      
    // SNIP, this doesn't make sense, and you're overriding the variable anyway

      
    $b $doc->createElement"author" );
      
      
    $name $doc->createElement"name" );
      
    $name->appendChild($doc->createTextNode$book['name'] ));
      
    $b->appendChild$name );
      
      
    $title $doc->createElement"title" );
      
    $title->appendChild($doc->createTextNode$book['title'] ));
      
    $b->appendChild$title );
      
      
    $publisher $doc->createElement"publisher" );
      
    $publisher->appendChild($doc->createTextNode$book['publisher'] ));
      
    $b->appendChild$publisher );
      
      
    $foo->appendChild$b ); // You were appending directly on the end of items...
    }

    echo 
    $doc->saveXML();
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  5. #5
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you! That got me straightened out. It's confusing at first, but one you finally catch on it's pretty easy


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
  •