Plain parsing
Parsing XML documents with eZ xml™ is simple, all you need to do is
pass the XML document to the domTree() function. The DOM object tree
will then be returned.
Below is the XML document used in this parser introduction.
<?xml version='1.0'?>
<doc>
<article>
<paragraph name='Introduction'>Paragraph contents</paragraph>
<paragraph name='Story'>Story paragraph contents</paragraph>
</article>
</doc>
The code snippet below shows how you can parse an XML document and create a DOM
tree. The DOM tree is then used to manipulate the document.
// instantiate the XML parser
$xml = new eZXML();
$testDoc = "<?xml version='1.0'?>
<doc>
<article>
<paragraph name='Introduction'>Paragraph contents</paragraph>
<paragraph name='Story'>Story paragraph contents</paragraph>
</article>
</doc>";
// $dom now contains the DOM object tree, false is returned if parsing was
// unsuccessful
$dom =& $xml->domTree( $testDoc );
Finding nodes
To find special nodes in the document you can use the elementsByName()
function. This will return an array of DOM nodes. The code snippet below
shows how you can find all paragraphs in the XML document.
// $paragraphs is an array of paragraph DOM nodes
$paragraphs =& $dom->elementsByName( "paragraph" );
Fetching content
When you have all the nodes you need you can access the attributes and
content of these items. All nodes have a name, this should in this example
be paragraph as we only fetched nodes by that name. The function
attributeValue() is used to fetch the value of an attribute on the
current node. To fetch the contents of a node directly you can use the
textContent() function. Note that textcontent only works if it's
a plain text node. Normally you would have to check all the children of the
node if you expect subnodes of mixed type.
foreach ( $paragraphs as $paragraph )
{
// get the name of the item, should be paragraph
print( "New " . $paragraph->name() );
// print the value of the name attribute
print( "Name: " . $paragraph->attributeValue( "name" ) );
// get the text content of the DOM node
print( "Content: " . $paragraph->textContent() );
}
This is the result of the above code:
New paragraph:
Name: Introduction
Content: Paragraph contents
New paragraph:
Name: Story
Content: Story paragraph contents
|