Problems getting the nodeName with DomDocument
| From: | Kenneth Andresen | Date: | Fri, 23 Dec 2005 02:53:12 +0000 |
| Subject: | Problems getting the nodeName with DomDocument | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-227826@lists.php.net to get a copy of this message | ||
I am having problems with the following functions where my return simply is "#text Joe #text Smith #text unknown", it should have read
firstname Joe lastname Smith address unknown
What am I doing wrong?
<?php
$xmlstring = "<element><firstname>Joe</firstname><lastname>Smith</lastname><address>unknown</address></element>";
$domdoc = new DomDocument();
$domdoc->loadXML($xmlstring);
$domroot = $domdoc->documentElement;
function process_children($node)
{
$children = $node->childNodes;
foreach ($children as $elem) {
if ($elem->nodeType == XML_TEXT_NODE) {
if (strlen(trim($elem->nodeValue))) {
$name = $elem->nodeName ;
$res = mb_convert_encoding(trim($elem->nodeValue), 'html', 'utf-8');
echo "$name $res \n"; }
} else if ($elem->nodeType == XML_ELEMENT_NODE) {
process_children($elem);
}
}
}
process_children($domroot);
?>