如何通过Domdocument PHP获取dom元素的第一级? [英] How get first level of dom elements by Domdocument PHP?

查看:225
本文介绍了如何通过Domdocument PHP获取dom元素的第一级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过Domdocument PHP获取第一级dom元素?

How get first level of dom elements by Domdocument PHP?

示例代码无效-摘自Q& A:http://stackoverflow.com/questions/1540302/how-to-get-nodes-in-first-level-using-php-domdocument

Example with code that not works - tooken from Q&A:http://stackoverflow.com/questions/1540302/how-to-get-nodes-in-first-level-using-php-domdocument

<?php
$str=<<< EOD
<div id="header">
</div>
<div id="content">
    <div id="sidebar">
    </div>
    <div id="info">
    </div>
</div>
<div id="footer">
</div>
EOD;

$doc = new DOMDocument();
$doc->loadHTML($str);
$xpath = new DOMXpath($doc);
$entries = $xpath->query("/");
foreach ($entries as $entry) {
    var_dump($entry->firstChild->nodeValue);
}
?>

谢谢, 优素福

推荐答案

可以通过以下方式访问根节点下的第一级元素:

The first level of elements below the root node can be accessed with

$dom->documentElement->childNodes

childNodes属性包含一个 DOMNodeList ,您可以用foreach进行迭代.

The childNodes property contains a DOMNodeList, which you can iterate with foreach.

请参见 DOMDocument::documentElement

这是一个方便属性,它允许直接访问作为文档文档元素的子节点.

This is a convenience attribute that allows direct access to the child node that is the document element of the document.

DOMNode::childNodes

一个DOMNodeList,包含该节点的所有子节点.如果没有子代,则这是一个空的DOMNodeList.

A DOMNodeList that contains all children of this node. If there are no children, this is an empty DOMNodeList.

由于childNodesDOMNode的属性,所以任何扩展DOMNode的类(这是DOM中的大多数类)都具有此属性,因此要获取DOMElement下的元素的第一级就是访问该DOMElement的childNode属性.

Since childNodes is a property of DOMNode any class extending DOMNode (which is most of the classes in DOM) have this property, so to get the first level of elements below a DOMElement is to access that DOMElement's childNode property.

请注意,如果对无效的HTML或部分文档使用DOMDocument::loadHTML(),则HTML解析器模块将添加带有html和body标签的HTML框架,因此在DOM树中,示例中的HTML将为

Note that if you use DOMDocument::loadHTML() on invalid HTML or partial documents, the HTML parser module will add an HTML skeleton with html and body tags, so in the DOM tree, the HTML in your example will be

<!DOCTYPE html … ">
<html><body><div id="header">
</div>
<div id="content">
    <div id="sidebar">
    </div>
    <div id="info">
    </div>
</div>
<div id="footer">
</div></body></html>

在遍历或使用XPath时必须考虑的问题.因此,使用

which you have to take into account when traversing or using XPath. Consequently, using

$dom = new DOMDocument;
$dom->loadHTML($str);
foreach ($dom->documentElement->childNodes as $node) {
    echo $node->nodeName; // body
}

将仅迭代<body> DOMElement节点.知道libxml将添加骨骼,因此您必须遍历<body>元素的childNodes以从示例代码中获取div元素,例如

will only iterate the <body> DOMElement node. Knowing that libxml will add the skeleton, you will have to iterate over the childNodes of the <body> element to get the div elements from your example code, e.g.

$dom->getElementsByTagName('body')->item(0)->childNodes

但是,这样做还将考虑所有空白节点,因此您必须确保将preserveWhiteSpace设置为false或查询正确的元素

However, doing so will also take into account any whitespace nodes, so you either have to make sure to set preserveWhiteSpace to false or query for the right element nodeType if you only want to get DOMElement nodes, e.g.

foreach ($dom->getElementsByTagName('body')->item(0)->childNodes as $node) {
    if ($node->nodeType === XML_ELEMENT_NODE) {
        echo $node->nodeName;
    }
}

或使用XPath

$dom->loadHTML($str);
$xpath = new DOMXPath($dom);
foreach ($xpath->query('/html/body/*') as $node) {
    echo $node->nodeName;
}

其他信息:

  • DOMDocument in php
  • Printing content of a XML file using XML DOM

这篇关于如何通过Domdocument PHP获取dom元素的第一级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆