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 ca
The Manual explains why:
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.
By all means, go for valid HTML & provide a DTD.
Quick fixes:
$dom->validate();
and put up with the errors (or fix them), afterwards you can use $dom->getElementById()
, regardless of the errors for some reason.$x = new DOMXPath($dom); $el = $x->query("//*[@id='bid']")->item(0);
validateOnParse
to true before loading the HTML, if would also work ;P.
$dom = new DOMDocument();
$html ='
Hello World.
';
$dom->validateOnParse = true; //
- 热议问题