note 59696 deleted from ref.domxml by sobak
| From: | sobak@php.net | Date: | Fri, 25 Nov 2016 05:39:24 +0000 |
| Subject: | note 59696 deleted from ref.domxml by sobak | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-207642@lists.php.net to get a copy of this message | ||
Note Submitter: Squiz
----
Re: websiterepairguys... Close but no cigar ;-)
As written it will not work if the repeated tags are somewhere other than the first node, i.e. the
following will not work:
<nodes>
<node>onething</node>
<node>something</node>
<node>something</node>
</nodes>
You must store the new node name when you get a new sibling that doesn't match the previous and
then it will work OK. Amended code:
function dom_to_array($domnode, &$array) {
$parent=$domnode;
$domnode = $domnode->firstChild;
$myname=$domnode->nodeName;
$x=1;
while (!is_null($domnode)) {
switch ($domnode->nodeType) {
case XML_ELEMENT_NODE: {
if ( !$domnode->hasChildNodes()) {
$array[$domnode->nodeName]='';
} else if ( $domnode->hasChildNodes() &&
$domnode->firstChild->nodeType==XML_TEXT_NODE) {
$array[$domnode->nodeName]=$domnode->firstChild->nodeValue;
} else if ( $domnode->hasChildNodes() ) {
$array_ptr = & $array[$domnode->nodeName];
dom_to_array($domnode, $array_ptr);
}
break;
}
}
$domnode = $domnode->nextSibling;
if($domnode->nodeName == $myname)
{
$domnode->nodeName.=($x++);
} else {
$myname = $domnode->nodeName;
}
}
}