| |
|
 |
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.
Installing eZ xml
eZ xml is a XML parser which is written in pure PHP, it doesn't need any external libraries. You can always get the latest version of eZ xml from http://developer.ez.no/developer/download/ezxml/stable
After downloading the tarball unpack it in your source directory. Then you just have to include the eZ xml class and you're ready to go.
// include eZ xml class
include_once( "ezxml/classes/ezxml.php" ); |
The XML file
Below is a simple XML file we want to parse. This is a fairly simple XML file but it contains attributes and subnodes. You should be able to tweak this example to fit your specific needs.
<?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
First we need to get the XML document into a PHP string. This is done here with direct initializing. Normally this is read from the database, file, socket or similar. After we've got the string with the XML document to parse we just have to send this to the eZXML::domTree() function which will return a document object tree.
// 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 ) ); |
Comment List
| Topic: |
Author: |
Time: |
|
parsing cdata
|
Daniel Williams
|
26.08.2002 17:12
|
|
How do you access the cdata within the <description> tags?
|
|
 |
|
|