Latest

Archive

Community news

C++

Communities and Content

Databases

Editorials

Emacs

General

HTML

Java

Notices

PHP

XML

Apache

C++

Database

General

HTML

Java

Javascript

Linux

Object oriented programming

Open source

Perl

PHP

Python

Ruby

SOAP

XML

Suggest a link

Advertise on zez

Contribute

Contact us

About zez


Parsing XML using PHP



Complete code listing

Below you will find the complete code listing for this example.


include_once( "ezxml/classes/ezxml.php" );

$xmlDocument =
"<?xml version=\"1.0\"?>
<document version=\"42\">
  <person>
    <firstname value=\"Bård\" />
    <lastname value=\"Farstad\" />
    <description>
    Coder.
    </description>
  </person>
  <person>
    <firstname value=\"Christoffer A.\" />
    <lastname value=\"Elo\" />
    <description>
    Coder.
    </description>
  </person>
</document>";

$tree =& eZXML::domTree( $xmlDocument, array( "TrimWhiteSpace" => true  ) );

foreach ( $tree->children as $document )
{
    // parse the document
    if ( $document->name == "document" )
    {
        // get the document version attribute
        foreach ( $document->attributes as $documentAttr )
        {
            if ( $documentAttr->name == "version" )
            {
                print( "Found document with version: " . $documentAttr->content . "<br>" );
            }
        }

        // parse all persons
        foreach ( $document->children as $person )
        {
            if ( $person->name == "person" )
            {
                print( "Found a new person <br>" );

                $firstName = "";
                $lastName = "";
                $descriptionName = "";
                
                // get the name and description
                foreach ( $person->children as $personAttribute )
                {
                    switch ( $personAttribute->name )
                    {
                        case "firstname" :
                        {
                            $firstName = getAttrValue( $personAttribute, "value" );
                        }break;

                        case "lastname" :
                        {
                            $lastName = getAttrValue( $personAttribute, "value" );
                        }break;

                        case "description" :
                        {
                            // get the description text
                            foreach ( $personAttribute->children as $description )
                            {
                                if ( $description->type == 3 )
                                {
                                    $description = $description->content;
                                }
                            }
                        }break;
                    }                    
                }

                print( "The persons firstname is: $firstName <br>" );
                print( "The persons lastname is: $lastName <br>" );
                print( "The persons description is: $description <br>" );                
            }            
        }
    }
}

/*!
  Function to fetch an attribute value.
  Will return the value of the attribute if found. False if not found.
*/
function getAttrValue( $node, $attrName )
{
    $ret = false;
     
    foreach ( $node->attributes as $nodeAttr )
    {
        if ( $nodeAttr->name == $attrName )
        {
            $ret = $nodeAttr->content;
        }
    }
    return $ret;
}


This code will produce the following output:

Found document with version: 42
Found a new person 
The persons firstname is: Bård 
The persons lastname is: Farstad 
The persons description is: Coder. 
Found a new person 
The persons firstname is: Christoffer A. 
The persons lastname is: Elo 
The persons description is: Coder2. 


Using XML is simple and straightforward with the eZ xml class. You don't need any external libraries, the class produce the same document tree as you would get from the XML functions in PHP (which all need external libraries). It is also the only PHP XML parser class which returns the same object tree as the library functions, making it easy to use your programs both on sites where XML is compiled into PHP and sites where it isn't.

You can learn more about XML and related technologies at http://www.w3.org/. The XML specification is located at this url http://www.w3.org/TR/REC-xml


<< Previous page | 1 | 2 | < 3 > | Printer-friendly page |

Comment List


Topic: Author:
Time:
parsing cdata Daniel Williams 26.08.2002 17:12

How do you access the cdata within the <description> tags?




Forgot your password?

Register a new user

Results

Polls