Re: DOM Element default ID attribute

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux