Hello
I have a problem with the DOM. When I try functions in the manual (http://www.zend.com/manual/ref.domxml.php) they usually don´t work or the methods are slightly different. Take a look at this example.
An XML file, lakes.xml:
<?xml version="1.0" encoding="UTF-8"?>
<attributes>
<lake id="1">
<area>5935174</area>
<perimeter>27205</perimeter>
</lake>
<lake id="2">
<area>2903033</area>
<perimeter>13935</perimeter>
</lake>
<lake id="3">
<area>89784025</area>
<perimeter>73851</perimeter>
</lake>
</attributes>
A PHP file, test.php:
<?
// data file
$file = "lakes.xml";
// create a document object
$dom = xmldocfile($file);
// get XML version
echo "<b>Version: </b>" . $dom->version . "<br>\n";
// get XML encoding
echo "<b>Encoding: </b>" . $dom->encoding . "<br>\n";
// get whether standalone file
echo "<b>Standalone: </b>" . $dom->standalone . "<br>\n";
// get XML URL
echo "<b>URL: </b>" . $dom->url . "<br>\n";
// get XML character set
echo "<b>Character set: </b>" . $dom->charset . "<br>\n";
// get reference to root node
$root = domxml_root($dom);
// get name of node
echo "<b>root type: </b>" . $root->type . "<br>\n";
echo "<b>root name: </b>" . $root->tagname . "<br>\n";
// get children of node, as array
$children = $root->children();
// get first child node
$firstChild = $children[0];
// let's see a few node properties
// get name of first child node
echo "<b>firstChild name: </b>" . $firstChild->tagname . "<br>\n";
// get content of first child node
echo "<b>firstChild content: </b>" . $firstChild->content . "<br>\n";
// get type of first child node
echo "<b>firstChild type: </b>" . $firstChild->type . "<br>\n";
$morechildren = $firstChild->children();
$moreFirstChild = $morechildren[0];
echo "<b>moreFirstChild name: </b>" . $moreFirstChild->tagname . "<br>\n";
echo "<b>moreFirstChild content: </b>" . $moreFirstChild->content . "<br>\n";
echo "<b>moreFirstChild type: </b>" . $moreFirstChild->type . "<br>\n";
// go back up the tree!
// get parent of child
$parentNode = $firstChild->parent();
// check it
echo "<b>parentNode name: </b>" . $parentNode->tagname . "<br>\n";
?>
Note that I have use "tagname" instead of "name" and that "content" gives no result. The output I get is:
Version: 1.0
Encoding: UTF-8
Standalone: -1
URL: lakes.xml
Character set: 1
root type: 1
root name: attributes
firstChild name: lake
firstChild content:
firstChild type: 1
moreFirstChild name: area
moreFirstChild content:
moreFirstChild type: 1
parentNode name: attributes
This is my setup:
Red Hat 7.2
PHP 4.1.2
expat 1.95.2
mod_xslt 1.5
sablot 0.82
libxml 2.4.16
zlib 1.1.3
Am I the only one with this problem (different function names)? "->content" gives my nothing, what is the correct call? I know about the issues with whitespeces so lakes.xml is one long string, I only formatted it here for easier reading.
What I want to do is pretty simple. From the XML file lakes.xml, do a search for a specific lake (i.e id=2), present all it´s children in HTML. Any pointers on how to do that? I have read a few articles that explain DOM in PHP but they all use function calls that doesn´t work for me.