note 71493 deleted from ref.domxml by sobak
| From: | sobak@php.net | Date: | Fri, 25 Nov 2016 05:39:34 +0000 |
| Subject: | note 71493 deleted from ref.domxml by sobak | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-207643@lists.php.net to get a copy of this message | ||
Note Submitter:
----
Referenced array functions drive me crazy for one reason or another (personal issue I guess). So
for any others like me, here's my modification (thanks to the original posters below for the
base to work on!)
I haven't tested this on much more than simple XML files, so there's probably a few ways
to break this - I'm also thinking this could probably be rewritten to be more efficient also,
but it's working quite well for me thus far.
<?php
function xml2array($domnode)
{
$nodearray = array();
$domnode = $domnode->firstChild;
while (!is_null($domnode))
{
$currentnode = $domnode->nodeName;
switch ($domnode->nodeType)
{
case XML_TEXT_NODE:
if(!(trim($domnode->nodeValue) == "")) $nodearray['cdata'] =
$domnode->nodeValue;
break;
case XML_ELEMENT_NODE:
if ($domnode->hasAttributes() )
{
$elementarray = array();
$attributes = $domnode->attributes;
foreach ($attributes as $index => $domobj)
{
$elementarray[$domobj->name] = $domobj->value;
}
}
break;
}
if ( $domnode->hasChildNodes() )
{
$nodearray[$currentnode][] = xml2array($domnode);
if (isset($elementarray))
{
$currnodeindex = count($nodearray[$currentnode]) - 1;
$nodearray[$currentnode][$currnodeindex]['@'] = $elementarray;
}
} else {
if (isset($elementarray) && $domnode->nodeType != XML_TEXT_NODE)
{
$nodearray[$currentnode]['@'] = $elementarray;
}
}
$domnode = $domnode->nextSibling;
}
return $nodearray;
}
?>