You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by Neil Sherman <ns...@aimltd.co.uk> on 2001/12/12 16:14:33 UTC

Locating an XML element by attribute value

Hi all!

I was just curious as to whether there is an easy way to locate an element 
within my document by one of its attribute values.

e.g.

<XML_DOC>
	<MY_ELEMENT id='100'>Some text</MY_ELEMENT>
	<MY_ELEMENT id='200'>Some more text</MY_ELEMENT>
	<INDEX id='200'>
</XML_DOC>

In the above document I can read the id value of the index element, but 
what I would then like to be able to do is a simple query such as find my 
the "MY_ELEMENT" which has "200" for its ID.  At the moment I am having to 
obtain a full list of "MY_ELEMENT" nodes and cycle through each one 
checking it's id against the id I have from the <INDEX> element.  This is 
incredibly wasteful if I have to look up more than one item in the 
<MY_ELEMENT> list!

Thanks for the help

Neil



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


Re: Locating an XML element by attribute value

Posted by "Jason E. Stewart" <ja...@openinformatics.com>.
"Neil Sherman" <ns...@aimltd.co.uk> writes:

> In the above document I can read the id value of the index element, but 
> what I would then like to be able to do is a simple query such as find my 
> the "MY_ELEMENT" which has "200" for its ID.  At the moment I am having to 
> obtain a full list of "MY_ELEMENT" nodes and cycle through each one 
> checking it's id against the id I have from the <INDEX> element.  This is 
> incredibly wasteful if I have to look up more than one item in the 
> <MY_ELEMENT> list!

Two choices:

* use DOM, and make the attribute you want to search on type ID, and
  use the getElementById() method

* build a hash table of all the elements and the attribute values, and
  look it up in your hash table.

jas.

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org