一个DOMNodeList,包含该节点的所有子节点.如果没有子代,则这是一个空的DOMNodeList. A DOMNodeList that contains all children of this node. If there are no children, this is an empty DOMNodeList.由于childNodes是DOMNode的属性,所以任何扩展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或查询正确的元素 nodeType ,如果您只想获取DOMElement节点,例如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;}其他信息: PHP中的DOMDocument 使用以下命令打印XML文件的内容XML DOM DOMDocument in phpPrinting content of a XML file using XML DOM 这篇关于如何通过Domdocument PHP获取dom元素的第一级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-25 17:54