[jdom-interest] reading name-value pairs

Matthew MacKenzie matt at xmlglobal.com
Mon May 14 09:13:36 PDT 2001


List sqlqries = ...;

for (int i=0; i<sqlqries.size(); i++) {
    Element sqlq = (Element)sqlqries.get(i);
    // sqlq is now an atomit unit, so it can be assumed that
    // name and query are associated.
    String name = sqlq.getChildTextTrim("name");
    String qry = sqlq.getChildTextTrim("query");
    System.out.println(name + ": " + qry);
}

JDOM knows that a certain name element belongs with a certain query element
because they are peers in the tree structure, so the trick is to select the
lowest common
parent element, in this case sqlquery.

If you want to specifically only access the sqlquery with a name of
QueryTenants, you need to use XPath (//sqlquery[name = "QueryTenants"]).
See jdom-contrib or www.werken.com.

Cheers,

-Matt

----- Original Message -----
From: "Michael Kastner" <kastner at galt.de>
To: <jdom-interest at jdom.org>
Sent: Monday, May 14, 2001 7:40 AM
Subject: [jdom-interest] reading name-value pairs


> Hello,
>
> I am using JDOM for reading and traversing configuration files for web
> applications. At the moment I am stuck with a kind of silly problem, but I
> haven't found a solution so far.
>
> Suppose I have this code:
>
> <sqlquery>
>     <name>
>         QueryHouses
>     </name>
>     <query>
>         SELECT * FROM houses
>     </query>
> <sqlquery>
> <sqlquery>
>     <name>
>         QueryTenants
>     </name>
>     <query>
>         SELECT * FROM tenants
>     </query>
> <sqlquery>
>
> How can I access the second query by its name? Or, to make it clear: how
can
> I tell JDOM that all tags within <sqlquery> (i.e. "QueryTenants" and
"SELECT
> * FROM tenants") belong together?
>
> Maybe someone can help?
>
> Michael Kastner
> galt.de
>
> _______________________________________________
> To control your jdom-interest membership:
>
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
t.com
>




More information about the jdom-interest mailing list