©
This document uses PHP Chinese website manual Release
(PHP 5, PHP 7)
DOMDocument::getElementById — Searches for an element with a certain id
$elementId
)This function is similar to DOMDocument::getElementsByTagName but searches for an element with a given id.
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.
elementId
The unique id value for an element.
Returns the DOMElement or NULL
if the element is
not found.
Example #1 DOMDocument::getElementById() Example
以下范例使用了 book.xml,其内容如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE books [ <!ELEMENT books (book+)> <!ELEMENT book (title, author+, xhtml:blurb?)> <!ELEMENT title (#PCDATA)> <!ELEMENT blurb (#PCDATA)> <!ELEMENT author (#PCDATA)> <!ATTLIST books xmlns CDATA #IMPLIED> <!ATTLIST books xmlns:xhtml CDATA #IMPLIED> <!ATTLIST book id ID #IMPLIED> <!ATTLIST author email CDATA #IMPLIED> ]> <?xml-stylesheet type="text/xsl" href="style.xsl"?> <books xmlns="http://books.php/" xmlns:xhtml="http://www.w3.org/1999/xhtml"> <book id="php-basics"> <title>PHP Basics</title> <author email="jim.smith@basics.php">Jim Smith</author> <author email="jane.smith@basics.php">Jane Smith</author> <xhtml:blurb><![CDATA[ <p><em>PHP Basics</em> provides an introduction to PHP.</p> ]]></xhtml:blurb> </book> <book id="php-advanced"> <title>PHP Advanced Programming</title> <author email="jon.doe@advanced.php">Jon Doe</author> </book> </books>
<?php
$doc = new DomDocument ;
// We need to validate our document before refering to the id
$doc -> validateOnParse = true ;
$doc -> Load ( 'book.xml' );
echo "The element whose id is 'php-basics' is: " . $doc -> getElementById ( 'php-basics' )-> tagName . "\n" ;
?>
以上例程会输出:
The element whose id is 'php-basics' is: book
[#1] ed at edgiardina dot com [2010-10-13 11:41:03]
Please note that if your HTML does not contain a doctype declaration, then getElementById will always return null.
[#2] paradox_haze at live dot de [2010-03-02 10:53:02]
Had some issues with getElementById() while searching for a specific element in a XHTML document.
I wrote a small function what was solving my problem:
<?php
function getElementById($id)
{
$xpath = new DOMXPath($this->domDocument);
return $xpath->query("/
$xmlDom = new DOMDocument('1.0', 'utf-8');
$xmlDom->formatOutput = true; // we want a nice output
// create a root
$eltRoot = $xmlDom->createElement("root");
$xmlDom->appendChild($eltRoot);
$eltChild = $xmlDom->createElement("child");
$eltRoot->appendChild($eltChild);
// add a id attribute
$attr = $xmlDom->createAttribute("xml:id"); // needs xml prefix or getElementById won't work
$eltChild->appendChild($attr);
/// create the text node and append to the created element
$tNode = $xmlDom->createTextNode("id_8120528");
$attr->appendChild($tNode);
$eltChild->setIdAttribute("xml:id", true); // VERY IMPORT or getElementById won't work
// add a id attribute
$attr = $xmlDom->createAttribute("status");
$eltChild->appendChild($attr);
/// create the text node and append to the created element
$tNode = $xmlDom->createTextNode("partial");
$attr->appendChild($tNode);
// add a subchild
$eltSub = $xmlDom->createElement("sub_child");
$eltChild->appendChild($eltSub);
$tNode = $xmlDom->createTextNode("Some Data");
$eltSub->appendChild($tNode);
$id = null;
$id = $xmlDom->getElementById("id_8120528");
assert ($id != null);
$strId = $id->getAttribute("xml:id"); // bug? empty
$strStatus = $id->getAttribute("status"); // this works!
assert ($id !=null);
$xmlDom->save("./_data/test.xml");
$xmlDom->load("./_data/test.xml"); // reloading fixes the problem
$nodeRoot = $xmlDom->getElementsByTagName("root");
if ($nodeRoot->length > 0) {
$eltRoot = $nodeRoot->item(0);
}
assert($eltRoot != null);
$id = null;
$id = $xmlDom->getElementById("id_8120528");
assert ($id != null);
$strId = $id->getAttribute("xml:id"); // this works now!
$strStatus = $id->getAttribute("status"); // this works!
?>
[#4] guillaume dot crico at gmail dot com [2008-10-01 07:55:35]
You don't want to use "xml:id" ?
Here is the relaxNG trick (with a generic schema):
(tested with libxml 2.6.26)
<?php
$doc = new DOMDocument();
$doc->load(...);
$rng = '
<grammar xmlns="http://relaxng.org/ns/structure/1.0" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
<start>
<element>
<anyName/>
<ref name="anythingID"/>
</element>
</start>
<define name="anythingID">
<zeroOrMore>
<choice>
<element>
<anyName/>
<ref name="anythingID"/>
</element>
<attribute name="id">
<data type="ID"/>
</attribute>
<zeroOrMore>
<attribute><anyName/></attribute>
</zeroOrMore>
<text/>
</choice>
</zeroOrMore>
</define>
</grammar>
';
$doc->relaxNGValidateSource($rng);
var_dump($doc->getElementById('id1'));
?>
Note that ID values must be valid ones :
- integers do no work!
- @see http://www.w3.org/TR/REC-xml/#id
- => (Letter | '_' | ':') ( Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender )*
[#5] simon at somewhere dot com [2007-01-29 20:07:06]
SAVE YOURSELF A MAJOR HEADACHE AND A LOT OF SEARCHING THROUGH DOCUMENTATION -
Instead of using $object->setAttribute('id', 'id_name_here')
USE THIS: $object->setAttribute('xml:id', 'id_name_here')
Then, to get the node value: $domDocumentObject->getElementById('id_name_here');
The xml:id attribute should AUTOMATICALLY be defined!!
Woohoo! That was easy......
[#6] jonbarnett at gmail dot com [2006-11-01 08:36:35]
If your XML document does not have a DTD that defines the "id" attribute as an ID, then the easiest thing to do is to use XPath->query() to find an element that matches "//[@id='x']"
[#7] Tangui dot Le-Pense at laposte dot net [2006-04-08 06:00:36]
Validating a document from a DTD so as to use getElementById is sometimes impossible (for example when the head and body elements are not included yet in a XHtml document : the validation failed).
Fortunately, xml:id is supported by this function :)
That may be useful.
http://www.w3.org/TR/xml-id/
[#8] [2005-12-20 07:04:59]
If you're trying to use getElementById with a xml file validated on a xsd file you must first use the schemaValidate function or getElementById will return null
Example:
$dom = new DomDocument();
$dom->load("users.xml");
$dom->schemaValidate("users.xsd");
$curruser = $dom->getElementById($user->name);
[#9] bart at mediawave dot nl [2005-09-11 04:46:24]
It seems getElementById works fine without setting validateOnParse to true. Which is nice since setting this to true caused some performance problems with my script.