Found at: http://publish.ez.no/article/articleprint/76/ |
Parsing XML using PHP |
Parsing XML is a very common task when programming for the web. This article will show you how to use the eZ xml parser to handle XML documents.
// include eZ xml class include_once( "ezxml/classes/ezxml.php" ); |
<?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> |
// Initialize the XML string
$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>";
// create the document object tree
$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>" );
}
}
// find persons here
}
} |
function getAttrValue( $node, $attrName )
{
$ret = false;
foreach ( $node->attributes as $nodeAttr )
{
if ( $nodeAttr->name == $attrName )
{
$ret = $nodeAttr->content;
}
}
return $ret;
} |
// 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>" );
}
} |
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;
} |
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. |