Re: DOM Element default ID attribute

From: Date: Thu, 22 Feb 2007 11:21:04 +0000
Subject: Re: DOM Element default ID attribute
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-249218@lists.php.net to get a copy of this message
Eli wrote:
Let me try to be more clear.. Say you got the element <elem key="peter"> , then I want the DOMDocument to automatically convert the 'key' attribute to an ID-Attribute, as done with DOMElement::setIdAttribute() function. The ID-Attribute is indexed and can be quickly gotten via DOMDocument::getElementById() function. I'm trying to avoid looping on all nodes overriding the importNode() and __construct() methods of DOMDocument.
Add a DTD to the document defining your attribute as an ID. $xml = <<<EOXML <?xml version="1.0"?> <!DOCTYPE note [ <!ATTLIST elem key ID #IMPLIED> ]> <doc> <elem key="peter">This is Peter</elem> <elem key="sam">This is Sam</elem> <elem key="mike">This is Mike</elem> </doc> EOXML; $dom = new DOMDocument(); $dom->loadXML($xml); if ($elem = $dom->getElementByID("sam")) { print $elem->textContent; } else { print "Element not found"; } Rob

« previous php.general (#249218) next »