Click to See Complete Forum and Search --> : php5 - dom xml difficulty


Snx
10-27-2005, 08:13 PM
can someone help me understand why i get nothing if I change $root from documentElement to getElementsByTagName('tagname')? the result when the function is called but the echo returns empty...

snx!



$dom = new DomDocument();
$dom->load('xml/index.xml');
$root = $dom->documentElement; // <----- arghh!!!
process_children($root);

function process_children($node)
{
$children = $node->childNodes;

foreach ($children as $elem) {
if ($elem->nodeType == XML_TEXT_NODE) {
if (strlen(trim($elem->nodeValue))) {
echo trim($elem->nodeValue)."\n";
}
} else if ($elem->nodeType == XML_ELEMENT_NODE) {
process_children($elem);
}
}
}

_theworks
11-07-2005, 05:08 PM
have u tried using simpleXML_load_file ? simpleXML comes standard in php5 also you might want to read this Zend Technologies - PHP 5 In Depth - SimpleXML (http://www.zend.com/php5/articles/php5-simplexml.php)

Weedpacket
11-07-2005, 08:21 PM
"the result when the function is called but".... The result what? Is the same? Isn't the same? Is correct? Is wrong? Is orange?

I get output when I use the code you posted. If I change documentElement to getElementsByTagname I get an error, but that's not surprising because getElementsByTagname doesn't return an element.

marque
11-17-2005, 06:41 PM
hey snx,

getElementsByTagName returns an node list not an element hense the plural "Elements"

use item method eg getElementsByTagName('tagname')->item(0) which returns an element that you can use in your function.

andrew embassy
12-06-2005, 02:20 PM
hey snx,

getElementsByTagName returns an node list not an element hense the plural "Elements"

use item method eg getElementsByTagName('tagname')->item(0) which returns an element that you can use in your function.

Damn, I was so excited to be able to answer this, and here Marque nails it.