Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » JDOM User »

Re: [jdom-interest] check if an element exists

Laurent Bihanic

2007-01-18

Replies:

> You can use XPath, for example searching for
> //user[@(protected)>']

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[@(protected)']");
...
String userName = ...
xpath.setVariable("username", userName);
Element e = xpath.selectSingleNode(doc);

Laurent


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[@(protected)>']
>
> Regards
> Grzegorz Kaczor
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> To control your jdom-interest membership:
>> http://www.jdom.org/mailman/options/jdom-interest/youraddr@(protected)
>

--
           wWw
          (o o)
-------------ooO-(_)-Ooo-----------------------------------------------
Laurent Bihanic           | Tel: +33 (0)1 70.92.47.90 (direct)
Atos Origin System Integration |    +33 (0)1 55.91.20.00
Consulting & Solutions Tech.  | Fax: +33 (0)1 55.91.22.31
Les Miroirs - Bat. C       |
18, avenue d'Alsace        |
F-92926 La Defense Cedex     | E-mail: laurent.bihanic@(protected)
-----------------------------------------------------------------------

Very funny, Scotty. Now beam down my clothes.


DISCLAIMER:
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?
_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@(protected)
©2008 junlu.com - Jax Systems, LLC, U.S.A.