PHP HTML DOMDocument getElementById 问题 [英] PHP HTML DOMDocument getElementById problems
问题描述
这里的 PHP 解析有点新,但我似乎无法让 PHP 的 DOMDocument 返回明显可识别的节点.加载的 HTML 将来自网络",因此不一定保证符合 XML,但我尝试以下操作:
A little new to PHP parsing here, but I can't seem to get PHP's DOMDocument to return what is clearly an identifiable node. The HTML loaded will come from the 'net so can't necessarily guarantee XML compliance, but I try the following:
<?php
header("Content-Type: text/plain");
$html = '<html><body>Hello <b id="bid">World</b>.</body></html>';
$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
$dom->validateOnParse = true;
/*** load the html into the object ***/
$dom->loadHTML($html);
var_dump($dom);
$belement = $dom->getElementById("bid");
var_dump($belement);
?>
虽然我没有收到任何错误,但我只收到以下输出:
Though I receive no error, I only receive the following as output:
object(DOMDocument)#1 (0) {
}
NULL
我是否应该无法查找 <b>
标签,因为它确实有一个 id?
Should I not be able to look up the <b>
tag as it does indeed have an id?
推荐答案
手册 解释了原因:
要使此函数工作,您需要使用 DOMElement->setIdAttribute() 或 DTD 设置一些 ID 属性,该 DTD 将属性定义为 ID 类型.在后一种情况下,您需要在使用此函数之前使用 DOMDocument->validate() 或 DOMDocument->validateOnParse 验证您的文档.
For this function to work, you will need either to set some ID attributes with DOMElement->setIdAttribute() or a DTD which defines an attribute to be of type ID. In the later case, you will need to validate your document with DOMDocument->validate() or DOMDocument->validateOnParse before using this function.
无论如何,选择有效的 HTML &提供 DTD.
By all means, go for valid HTML & provide a DTD.
快速修复:
- 调用
$dom->validate();
并忍受错误(或修复它们),之后您可以使用$dom->getElementById()
,不管出于某种原因的错误. - 如果您不想验证,请使用 XPath:
$x = new DOMXPath($dom);$el = $x->query("///*[@id='bid']")->item(0);
- 想想看:如果你只是在加载 HTML 之前将
validateOnParse
设置为 true ,如果也可以工作;P
- Call
$dom->validate();
and put up with the errors (or fix them), afterwards you can use$dom->getElementById()
, regardless of the errors for some reason. - Use XPath if you don't feel like validing:
$x = new DOMXPath($dom); $el = $x->query("//*[@id='bid']")->item(0);
- Come to think of it: if you just set
validateOnParse
to true before loading the HTML, if would also work ;P
.
$dom = new DOMDocument();
$html ='<html>
<body>Hello <b id="bid">World</b>.</body>
</html>';
$dom->validateOnParse = true; //<!-- this first
$dom->loadHTML($html); //'cause 'load' == 'parse
$dom->preserveWhiteSpace = false;
$belement = $dom->getElementById("bid");
echo $belement->nodeValue;
在此处输出世界".
这篇关于PHP HTML DOMDocument getElementById 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!