First, note that the function name is getElementById, not getElementByID, or getElementsById.
Second, this function is a member of DOMDocument, not DOMElement. If you get a DOMElement parent and try to get its child element using parent->getElementById(), you will get the following error:
Fatal error: Call to undefined method DOMElement::getElementById() in …
This is unlike Javascript or other programming languages in which dom element has its getElementById function.
Third, the function returns null if there is no doctype declaration in the parsed xml file even there is an element with that id in the xml file.
Fourth, if you don’t specify the doctype declaration in the xml file, you can write the id attribute as xml:id=”attributevalue”. This way, getElementById can return the element successfully. But loading the xml will trigger a warning message:
Warning: DOMDocument::load(): xml:id : attribute value xxx is not an NCName in file
Comments are closed, but trackbacks and pingbacks are open.