示例#1
0
function dom2array_full($node)
{
    $result = array();
    if ($node->nodeType == XML_TEXT_NODE) {
        $result = $node->nodeValue;
    } else {
        if ($node->hasAttributes()) {
            $attributes = $node->attributes;
            if (!is_null($attributes) && count($attributes)) {
                foreach ($attributes as $index => $attr) {
                    $result[$attr->name] = $attr->value;
                }
            }
        }
        if ($node->hasChildNodes()) {
            $children = $node->childNodes;
            for ($i = 0; $i < $children->length; $i++) {
                $child = $children->item($i);
                if ($child->nodeName != '#text') {
                    if (!isset($result[$child->nodeName])) {
                        $result[$child->nodeName] = dom2array($child);
                    } else {
                        $aux = $result[$child->nodeName];
                        $result[$child->nodeName] = array($aux);
                        $result[$child->nodeName][] = dom2array($child);
                    }
                }
            }
        }
    }
    return $result;
}
示例#2
0
function dom2array($node)
{
    $res = array();
    if ($node->nodeType == XML_TEXT_NODE) {
        $res = $node->nodeValue;
    } else {
        if ($node->hasAttributes()) {
            $attributes = $node->attributes;
            if (!is_null($attributes)) {
                $res['@attributes'] = array();
                foreach ($attributes as $index => $attr) {
                    $res['@attributes'][$attr->name] = $attr->value;
                }
            }
        }
        if ($node->hasChildNodes()) {
            $children = $node->childNodes;
            for ($i = 0; $i < $children->length; $i++) {
                $child = $children->item($i);
                $res[$child->nodeName] = dom2array($child);
            }
        }
    }
    return $res;
}