[jdom-interest] check if an element exists

Laurent Bihanic laurent.bihanic at atosorigin.com
Thu Jan 18 03:56:59 PST 2007

 > You can use XPath, for example searching for
 > //user[@username='<user_name>']

You can even compile your XPath expression once and reuse it for many queries 
by using a variable:

import org.jdom.xpath.XPath;
XPath xpath = XPath.newInstance("//user[@username='$username']");
String userName = ...
xpath.setVariable("username", userName);
Element e = xpath.selectSingleNode(doc);


Grzegorz Kaczor a écrit :
>> What could be the easiest way to check if a certain username exists?
>> I have an xml like this:
>> <tomcat-users>
>>    <role rolename="tomcat"/>
>>    <role rolename="role1"/>
>>    <role rolename="manager"/>
>>    <user username="tomcat" password="tomcat" roles="tomcat"/>
>>    <user username="both" password="tomcat" roles="tomcat,role1"/>
>>    <user username="role1" password="tomcat" roles="role1"/>
>> </tomcat-users>
>> When it comes to check if a certain user with a certain username 
>> exists, I don know an easy way to do that. Can someone help? Thnx.
> You can use XPath, for example searching for 
> //user[@username='<user_name>']
> Regards
> Grzegorz Kaczor
>> ------------------------------------------------------------------------
>> _______________________________________________
>> To control your jdom-interest membership:
>> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com

                 (o o)
Laurent Bihanic                | Tel: +33 (0)1 (direct)
Atos Origin System Integration |      +33 (0)1
Consulting & Solutions Tech.   | Fax: +33 (0)1
Les Miroirs - Bat. C           |
18, avenue d'Alsace            |
F-92926 La Defense Cedex       | E-mail: laurent.bihanic at atosorigin.com

Very funny, Scotty. Now beam down my clothes.

The opinions expressed are entirely my own and may not necessarily be
those of my employer.  Also, I am not now nor have I ever been a
lawyer.  My opinions are provided as-is with absolutely no warrantee of
merchantability or fitness for any particular use.  Besides, you can't
prove I typed this.  No body saw me type this.  Who says I typed this?

More information about the jdom-interest mailing list