File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes getting nodes with regular expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "getting nodes with regular expression" Watch "getting nodes with regular expression" New topic

getting nodes with regular expression

ravindra patil
Ranch Hand

Joined: Sep 01, 2006
Posts: 234

I do have xml like this

Now i want to get elements based on search criteria entered by user ..e.g if user enters a* then i want all the elements whose name value starts with a ... so i should get first and last elements similarly for dept..

before searching i have what attribute ( name or dept) to search and by pattern (attribute value a*b*) to search .

it is possible ? if yes please tell me
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

If you load the XML document into a DOM, then you can use XPath to search it. By default Java supports XPath 1.0, which has a "starts-with" string function so you can find elements whose Name attribute value starts with "a", for example. XPath 1.0 doesn't support regular expressions, but XPath 2.0 does. It looks to me like you might be able to use XPath 2.0 on your DOM if you could track down a suitable XPath 2.0 provider (XPathFactory allows you to load non-default providers) but I've never done that and never even seen anything which claims to do it.

You might find it easier just to traverse the elements in your DOM and compare the Name attributes using Java code for the regular expressions. You could use XPath to find all of the Name attributes and then apply the regular expressions to them in Java is what I mean.
I agree. Here's the link:
subject: getting nodes with regular expression
jQuery in Action, 3rd edition