[jdom-interest] Finding text for an element in an XML Doc

Scott Johnston scott at kintana.com
Thu Apr 19 08:49:25 PDT 2001


I actually have been meaning to ask about this. On a recent project I had
the same needs but even a step further. I needed to find specific nodes
based on their children. I ended up going to the road of partially
implementing XSLT searching. I didn't get to attribute specification but
this would be easy to add. Right now I did it with utility methods (not by
subclassing Element). The controlling utility method looks like this:

/**
 * Performs a hierarchy traversal
 * to find a child node. Format of the path is expected like:
 * <code>/root/path/to/child</code>. The method starts looking from the
parent
 * element passed to the method. Note that specifying a non-unique child
 * can cause unexpected results (method with return the first child matching
 * the path). To select children from a list use the query format:
 * <code>/root/path/to(child_node="value")/child</code>. Currently
 * this method only support AND queries in the format 
 * (node1="val1" and node2="val2").
 * 
 * @param parent the element to start with
 * @param path full path to descendant node
 * @return the matching element, null if none if found
 *
 * @see findChild(Element,String,String)
 */
public static Element getDescendant(Element parent, String path)

This allows you to get a descendant using "/path/to/element" or
"/path/to/element(child="value" and child="value")/blah/blah. As I
mentioned it currently only supports "and" queries and doesn't handle
attributes. This method was a nice generalization I could use in a
couple places that parsed config files and messages.

Anyway I would be happy to contribute code but I'm new to JDOM so I'm
not sure how I would go about doing that. Seems like it would be a serious
and possibly confusing change to the API. Thoughts?

hope all is well,

sj

> -----Original Message-----
> From: Mark Glass [mailto:mglass at datalinx.net]
> Sent: Thursday, April 19, 2001 8:07 AM
> To: jdom-interest
> Subject: [jdom-interest] Finding text for an element in an XML Doc
> 
> 
> Hi all,
>   I need some suggestions -- I'm working on an application 
> which reads 
> an XML document, determines the text in a node which is specified by 
> through a config file and returns the ttext. I intened to use XPath, 
> e.g. String elementText = 
> getKeyFieldText("/root/subelement1/childelement/Text()"). After doing 
> some investigation it appears that XPath is used only for 
> XSL. Are there 
> any libraries available that will allow items to be located in an XML 
> document?
> 
> Thanks in advance for your help..
> 
> Mark Glass
> 
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/yo
uraddr at yourhost.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20010419/8c787d27/attachment.htm


More information about the jdom-interest mailing list