SitePoint Sponsor

User Tag List

Results 1 to 1 of 1
  1. #1
    SitePoint Addict
    Join Date
    Nov 2005
    Location
    Moss, Norway.
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Accessing content and using iterated Objects in SimpleXML.

    This is an follow up of this thread:

    Problem with XML in CDATA tag (swf related)

    xml file used below:
    http://www.kjellbleivik.com//Books/P...ter07/sxml.xml

    It starts here with the following code:

    PHP Code:
    <?php
    $book
    =simplexml_load_file('http://www.kjellbleivik.com/Books/ProPHP/Chapter07/sxml.xml');
    /* 
    Accessing Element Nodes start here. 
    1.  Access the bookinfo child element of the book element
    */
    $bookinfo $book->bookinfo;
    print 
    $bookinfo '<br /> <br />';  //nothing printed.  WHY?  Explained in text.
    print 'But I am here. The line breaks seemingly worked.  This parser is funny';
    print 
    '<br /> <br />';
    $title $bookinfo->title;
    print 
    $title '<br /> <br />';   // now I am confused.  WHY?  Explained in text.
    ?>
    Here is the result: http://www.kjellbleivik.com/SpHelp/morgy2.php

    It continues here:
    PHP Code:
    <?php
    $book
    =simplexml_load_file('http://www.kjellbleivik.com/Books/ProPHP/Chapter07/sxml.xml');
    /* 
    Alternative.

    $book=simplexml_load_file('http://www.kjellbleivik.com/Books/ProPHP/Chapter07/sxml.xml','',LIBXML_NOCDATA);

    Merges CDATA nodes into text nodes.  A document using CDATA sections will be created with no CDATA 
    nodes, as these will now be converted into plain text nodes.  The LIBXML_NOCDATA flag is useful when 
    loading a document to be used for an XSL transformation.
    */
    $bookinfo $book->bookinfo;
    $title $bookinfo->title;
    $author $bookinfo->author;
    /* Objects examined with vardump */
    var_dump($title);
    print 
    '<br /> <br />';
    var_dump($author);
    print 
    '<br /> <br />';
    /*using print with element containing text-only content */
    print "Title: ".$title;
    print 
    '<br /> <br />';
    /*using print with element containing child elements */
    print "Author: ".$author;
    ?>
    Result: http://www.kjellbleivik.com/SpHelp/morgy3

    Confused?
    Everything depends on how objects are iterated in SimpleXML (in my version of PHP, 5.1.4 when this code snippet is written).

    Does this example help?

    PHP Code:
    <?php
    $doc
    = new simpleXMLElement("<root>some<child1>subtext</child1>thing</root>");
    print 
    $doc;
    ?>
    Result here.

    http://www.kjellbleivik.com/SpHelp/morgy4

    Note the mixed content, and only the content of the <root> node is printed. The <child1> node and its content is completely skipped. Also notice that the output does not contain any spaces and line feeds.

    We end the the example here with the following code:

    PHP Code:
    <?php
    $book
    =simplexml_load_file('http://www.kjellbleivik.com/Books/ProPHP/Chapter07/sxml.xml');
    $author $book->bookinfo->author;
    $children $author->children();
    foreach(
    $children AS $child) {
        print 
    $child '<br />';
    }
    print 
    '<br />';
    $props get_object_vars($author);
    foreach(
    $props AS $name=>$value) {   //Note => and not -> !!!
        
    print $name": "$value '<br />';
    }
    ?>
    Result here: http://www.kjellbleivik.com/SpHelp/morgy5

    Source with some modifications: Robert Richards (2006) chapter 7.

    Additional information:

    php.net: CLI. SimpleXML functions

    ZEND Developer zone: SimpleXML

    Published 2004-06-22

    DevShed: SimpleXML

    Published 10 Oct 2006

    IBM: SimpleXML processing with PHP
    Last edited by kgun; Jul 15, 2007 at 13:43. Reason: Additional code.


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
  •