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



Traverse the document tree

In this example we have a XML file which consists of a document with a specific version. The document contains persons with the attributes firstname, lastname and description. The code snippet finds the document version and prints it. It's pretty straight forward you just check all the top nodes of the document and look for the one named document.


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>" );
            }
        }

        // find persons here
    }
}


When you've found the document node you can start looking for persons. This is done in the same manner, check the children nodes and look for person.

To make the process of getting the attribute values simpler we write a helper function to fetch the attribute value from a node.


function getAttrValue( $node, $attrName )
{
    $ret = false;
     
    foreach ( $node->attributes as $nodeAttr )
    {
        if ( $nodeAttr->name == $attrName )
        {
            $ret = $nodeAttr->content;
        }
    }
    return $ret;
}


Now we're ready to parse the information describing the persons in this example. When we've found the person node we check all the subnodes and look for the nodes we want and fetch the information from these nodes.


// 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>" );
    }            
}


<< Previous page | 1 | < 2 > | 3 | Next page >> | 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