You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Just van den Broecke <ju...@justobjects.nl> on 2000/08/07 12:17:31 UTC

Re: Searching for an element

Hi,

For this purpose I wrote a DOM Query class. It uses a depth-first
TreeWalker combined with a Visitor callback. You instantiate the Query
with the target document and then add match criteria (like match by tag,
attribute name and or value, example, text etc). By calling
DOMQuery.query() the DOM tree is traversed and all the matchers AND-ed.
You can even do query by example or extend with your custom matchers.
The result is a Vector of Elements. The code would look like:

Document document;
// parse document

// Instantiate query
Query query = new Query(myDocument);		
query.addMatcherByTag("Product");
query.addMatcherByAttributeNameValue("Date", "6/1/2000");

// Execute query and return result
Vector result = query.query();

For most of my purposes this works well and relieves me from writing
custom queries. If you are interested I can send you the code. If there
is interest form others I can even put it online. I think there are W3C
XML standards in the make that would do the same kind of thing but I
wanted to make a simple/understandable implementation. (I now realize
that this is all not related to xerces...) thanks,

--Just

Just van den Broecke          just@justobjects.nl
Just Objects B.V.             tel. +31 65 4268627                
The Netherlands               http://www.justobjects.nl

This is how the code for your 

Charles Brown wrote:
> 
>   Try something like;
> 
>     // collection of product nodes matching date
>     Vector v;
> 
>     // parse XML using DOM
>     dom.parse( is );
> 
>     // get resulting document
>     Document doc = dom.getDocument();
> 
>     // get list of products
>     NodeList nl = doc.getElementsByTagName( "Product" );
> 
>     // find prducts with date, collect them in vector
>     for ( int i=0 ; i < nl.getLength() ; ++i ) {
> 
>       // get product node
>       Node n = nl.item(i);
> 
>       // get product's date attribute
>       Element e = (Element)n;
>       String date = e.getAttribute( "Date" );
> 
>       // get the title text, and see if it contains "java"
>       if ( date.equals( "6/1/2000" ) )
>         v.add( n );
>     } // for
> 
>     return v;
> 
> > -----Original Message-----
> > From: Robert La Ferla [mailto:robertlaferla@mediaone.net]
> > Sent: Sunday, August 06, 2000 11:44 PM
> > To: xerces-j-dev@xml.apache.org
> > Subject: Searching for an element
> >
> >
> > Developers,
> >
> > I need to find all elements that have a certain attribute value.  How do
> > I query the DOM tree for an element that matches some criteria.  e.g.
> > List all elements named "Product" whose "Date" attribute is "6/1/2000"
> >
> > Thanks,
> > Robert
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
> > For additional commands, e-mail: xerces-j-dev-help@xml.apache.org
> >
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-dev-help@xml.apache.org

--

Re: Searching for an element

Posted by Arnaud Le Hors <le...@us.ibm.com>.
Robert La Ferla wrote:
> 
> Hi,
> 
> I had expected the API to include that kind of query functionality.  Anyone know
> what W3C/XML have put together for this in terms of standards?  More
> importantly, will Xerces include them?

There was a proposal to add a method called
getElementsByAttributeValue() in DOM Level 3 but the DOM Working Group
thought it wasn't worth it. Several people have been asking for an XPath
based query API for the DOM, but nobody has ever made any concrete
proposal. W3C now has a WG on XML Query. The DOM WG is waiting for this
to see what kind of API could/should be provided for that.
-- 
Arnaud  Le Hors - IBM Cupertino, XML Technology Group

Re: Searching for an element

Posted by Robert La Ferla <ro...@mediaone.net>.
Hi,

I had expected the API to include that kind of query functionality.  Anyone know
what W3C/XML have put together for this in terms of standards?  More
importantly, will Xerces include them?

BTW - Just, I do like your simplified interface.  Please make it available
online...

Robert


Just van den Broecke wrote:

> Hi,
>
> For this purpose I wrote a DOM Query class. It uses a depth-first
> TreeWalker combined with a Visitor callback. You instantiate the Query
> with the target document and then add match criteria (like match by tag,
> attribute name and or value, example, text etc). By calling
> DOMQuery.query() the DOM tree is traversed and all the matchers AND-ed.
> You can even do query by example or extend with your custom matchers.
> The result is a Vector of Elements. The code would look like:
>
> Document document;
> // parse document
>
> // Instantiate query
> Query query = new Query(myDocument);
> query.addMatcherByTag("Product");
> query.addMatcherByAttributeNameValue("Date", "6/1/2000");
>
> // Execute query and return result
> Vector result = query.query();
>
> For most of my purposes this works well and relieves me from writing
> custom queries. If you are interested I can send you the code. If there
> is interest form others I can even put it online. I think there are W3C
> XML standards in the make that would do the same kind of thing but I
> wanted to make a simple/understandable implementation. (I now realize
> that this is all not related to xerces...) thanks,
>
> --Just
>
> Just van den Broecke          just@justobjects.nl
> Just Objects B.V.             tel. +31 65 4268627
> The Netherlands               http://www.justobjects.nl
>
> This is how the code for your
>
> Charles Brown wrote:
> >
> >   Try something like;
> >
> >     // collection of product nodes matching date
> >     Vector v;
> >
> >     // parse XML using DOM
> >     dom.parse( is );
> >
> >     // get resulting document
> >     Document doc = dom.getDocument();
> >
> >     // get list of products
> >     NodeList nl = doc.getElementsByTagName( "Product" );
> >
> >     // find prducts with date, collect them in vector
> >     for ( int i=0 ; i < nl.getLength() ; ++i ) {
> >
> >       // get product node
> >       Node n = nl.item(i);
> >
> >       // get product's date attribute
> >       Element e = (Element)n;
> >       String date = e.getAttribute( "Date" );
> >
> >       // get the title text, and see if it contains "java"
> >       if ( date.equals( "6/1/2000" ) )
> >         v.add( n );
> >     } // for
> >
> >     return v;
> >
> > > -----Original Message-----
> > > From: Robert La Ferla [mailto:robertlaferla@mediaone.net]
> > > Sent: Sunday, August 06, 2000 11:44 PM
> > > To: xerces-j-dev@xml.apache.org
> > > Subject: Searching for an element
> > >
> > >
> > > Developers,
> > >
> > > I need to find all elements that have a certain attribute value.  How do
> > > I query the DOM tree for an element that matches some criteria.  e.g.
> > > List all elements named "Product" whose "Date" attribute is "6/1/2000"
> > >
> > > Thanks,
> > > Robert
> > >
> > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
> > > For additional commands, e-mail: xerces-j-dev-help@xml.apache.org
> > >
> > >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
> > For additional commands, e-mail: xerces-j-dev-help@xml.apache.org
>
> --
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-dev-help@xml.apache.org