如何通过Domdocument获取第一层dom元素?
如何通过 Domdocument PHP 获取第一层 dom 元素?
代码不起作用的示例 - 取自问答:如何使用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);
}
?>
How get first level of dom elements by Domdocument PHP?
Example with code that not works - tooken from Q&A: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);
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根节点下面的第一级元素可以通过
childNodes 属性包含 进行访问
DOMNodeList
,您可以使用foreach
对其进行迭代。请参阅
DOMDocument::documentElement
和
DOMNode::childNodes
由于
childNodes
是DOMNode
的属性,任何扩展DOMNode
的类(这是 DOM 中的大多数类)都具有此属性,因此要获取DOMElement
下面的第一级元素是访问该 DOMElement 的 childNode 属性。请注意,如果您在无效的 HTML 或部分文档上使用 DOMDocument::loadHTML(),HTML 解析器模块将添加带有 html 和 body 标记的 HTML 骨架,因此在 DOM 树中,您的 HTML示例将是
您在遍历或使用 XPath 时必须考虑的示例。因此,使用
只会迭代
DOMElement 节点。知道 libxml 将添加骨架,您将必须迭代
元素的 childNodes 以从示例代码中获取 div 元素,例如,
但是,这样做也会考虑到任何空白节点,因此您必须确保将
preserveWhiteSpace
设置为 false 或查询正确的元素 nodeType 如果您只想获取DOMElement
节点,例如或使用 XPath
其他信息:
The first level of elements below the root node can be accessed with
The childNodes property contains a
DOMNodeList
, which you can iterate withforeach
.See
DOMDocument::documentElement
and
DOMNode::childNodes
Since
childNodes
is a property ofDOMNode
any class extendingDOMNode
(which is most of the classes in DOM) have this property, so to get the first level of elements below aDOMElement
is to access that DOMElement's childNode property.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 bewhich you have to take into account when traversing or using XPath. Consequently, using
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.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 getDOMElement
nodes, e.g.or use XPath
Additional information: