我有这个函数,它接受dom节点并从中创建一个数组。 但它只给出了我认为关于兄弟姐妹的页面的一部分,但是没有找到如何修改它:
function nodeToArray( $dom, $node)
{
if(!is_a( $dom, 'DOMDocument' ) || !is_a( $node, 'DOMNode' )) {
return false;
}
$array = false;
if( empty( trim( $node->localName ))) {// Discard empty nodes
return false;
}
if( XML_TEXT_NODE == $node->nodeType ) {
return $node->nodeValue;
}
foreach ($node->attributes as $attr) {
$array['@'.$attr->localName] = $attr->nodeValue;
}
foreach ($node->childNodes as $childNode) {
if ( 1 == $childNode->childNodes->length && XML_TEXT_NODE == $childNode->firstChild->nodeType ) {
$array[$childNode->localName] = $childNode->nodeValue;
} else {
if( false !== ($a = nodeToArray( $dom, $childNode))) {
$array[$childNode->localName] = $a;
}
}
}
return $array;
}
$dom = new DOMDocument();
$dom->loadHTML($response);
$a=nodeToArray($dom,$dom->getElementsByTagName("body")->item(0));
print "<pre>";
print_r($a);
print "</pre>";
由于