<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2650.12">
<TITLE>RE: [jdom-interest] Finding text for an element in an XML Doc</TITLE>
</HEAD>
<BODY>
<P><FONT SIZE=2>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:</FONT></P>
<P><FONT SIZE=2>/**</FONT>
<BR><FONT SIZE=2> * Performs a hierarchy traversal</FONT>
<BR><FONT SIZE=2> * to find a child node. Format of the path is expected like:</FONT>
<BR><FONT SIZE=2> * <code>/root/path/to/child</code>. The method starts looking from the parent</FONT>
<BR><FONT SIZE=2> * element passed to the method. Note that specifying a non-unique child</FONT>
<BR><FONT SIZE=2> * can cause unexpected results (method with return the first child matching</FONT>
<BR><FONT SIZE=2> * the path). To select children from a list use the query format:</FONT>
<BR><FONT SIZE=2> * <code>/root/path/to(child_node="value")/child</code>. Currently</FONT>
<BR><FONT SIZE=2> * this method only support AND queries in the format </FONT>
<BR><FONT SIZE=2> * (node1="val1" and node2="val2").</FONT>
<BR><FONT SIZE=2> * </FONT>
<BR><FONT SIZE=2> * @param parent the element to start with</FONT>
<BR><FONT SIZE=2> * @param path full path to descendant node</FONT>
<BR><FONT SIZE=2> * @return the matching element, null if none if found</FONT>
<BR><FONT SIZE=2> *</FONT>
<BR><FONT SIZE=2> * @see findChild(Element,String,String)</FONT>
<BR><FONT SIZE=2> */</FONT>
<BR><FONT SIZE=2>public static Element getDescendant(Element parent, String path)</FONT>
</P>
<P><FONT SIZE=2>This allows you to get a descendant using "/path/to/element" or</FONT>
<BR><FONT SIZE=2>"/path/to/element(child="value" and child="value")/blah/blah. As I</FONT>
<BR><FONT SIZE=2>mentioned it currently only supports "and" queries and doesn't handle</FONT>
<BR><FONT SIZE=2>attributes. This method was a nice generalization I could use in a</FONT>
<BR><FONT SIZE=2>couple places that parsed config files and messages.</FONT>
</P>
<P><FONT SIZE=2>Anyway I would be happy to contribute code but I'm new to JDOM so I'm</FONT>
<BR><FONT SIZE=2>not sure how I would go about doing that. Seems like it would be a serious</FONT>
<BR><FONT SIZE=2>and possibly confusing change to the API. Thoughts?</FONT>
</P>
<P><FONT SIZE=2>hope all is well,</FONT>
</P>
<P><FONT SIZE=2>sj</FONT>
</P>
<P><FONT SIZE=2>> -----Original Message-----</FONT>
<BR><FONT SIZE=2>> From: Mark Glass [<A HREF="mailto:mglass@datalinx.net">mailto:mglass@datalinx.net</A>]</FONT>
<BR><FONT SIZE=2>> Sent: Thursday, April 19, 2001 8:07 AM</FONT>
<BR><FONT SIZE=2>> To: jdom-interest</FONT>
<BR><FONT SIZE=2>> Subject: [jdom-interest] Finding text for an element in an XML Doc</FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> Hi all,</FONT>
<BR><FONT SIZE=2>> I need some suggestions -- I'm working on an application </FONT>
<BR><FONT SIZE=2>> which reads </FONT>
<BR><FONT SIZE=2>> an XML document, determines the text in a node which is specified by </FONT>
<BR><FONT SIZE=2>> through a config file and returns the ttext. I intened to use XPath, </FONT>
<BR><FONT SIZE=2>> e.g. String elementText = </FONT>
<BR><FONT SIZE=2>> getKeyFieldText("/root/subelement1/childelement/Text()"). After doing </FONT>
<BR><FONT SIZE=2>> some investigation it appears that XPath is used only for </FONT>
<BR><FONT SIZE=2>> XSL. Are there </FONT>
<BR><FONT SIZE=2>> any libraries available that will allow items to be located in an XML </FONT>
<BR><FONT SIZE=2>> document?</FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> Thanks in advance for your help..</FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> Mark Glass</FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> _______________________________________________</FONT>
<BR><FONT SIZE=2>> To control your jdom-interest membership:</FONT>
<BR><FONT SIZE=2>> <A HREF="http://lists.denveronline.net/mailman/options/jdom-interest/yo" TARGET="_blank">http://lists.denveronline.net/mailman/options/jdom-interest/yo</A></FONT>
<BR><FONT SIZE=2>uraddr@yourhost.com</FONT>
</P>
</BODY>
</HTML>