I'd like to expose a tree of POJOs via a simple "XPath" like language so that users can do something like /purchaseOrder/location[@city = 'Mountain View']
?
Asked
Active
Viewed 1,500 times
4

Donal Fellows
- 133,037
- 18
- 149
- 215

Ashwin Jayaprakash
- 2,168
- 24
- 29
-
Please give a little more detail on what you're trying to do. How would the user interact with your system? Would you have POJO's loaded in your program and then let the user "query" them? – pcalcao Oct 27 '11 at 23:18
-
Yes, the POJOs are pre-existing instances and loaded. However, there could be cases where 1 POJO's fields can get copied to another via XPath. – Ashwin Jayaprakash Oct 27 '11 at 23:20
2 Answers
4
I've looked at Apache JXpath which seems to be pretty decent and is somewhat maintained. I don't know how fast it is though.
I've also seen Jaxen which requires some extra work by the user as described here and by reading PMD's soure code. But Jaxen seems to have been abandoned by its authors. Also, I don't know what version of XPath is supported here.
Are there others?

Community
- 1
- 1

Ashwin Jayaprakash
- 2,168
- 24
- 29
2
If your POJOs are mapped to XML via JAXB metadata, then you could wrap the root object in an instance of javax.xml.bind.util.JAXBSource
and use the javax.xml.xpath
APIs to issue XPath queries against it.

bdoughan
- 147,609
- 23
- 300
- 400
-
That does not work. `JAXBSource source = new JAXBSource(jaxbContext, parent); Object result = expr.evaluate(source.getInputSource());` throws an error saying MalformedUrlException because the getInputSource() is all nulls – Ashwin Jayaprakash Nov 08 '11 at 00:25
-
I found out the proper way to do this using JAXB objects - http://www.devguli.com/blog/eng/jaxb-binder-and-xpath/ – Ashwin Jayaprakash Nov 08 '11 at 00:44
-
Or this http://jarfiller.com/guide/jaxb/xpath.xhtml. But you will find that JXPath is way faster – Ashwin Jayaprakash Nov 08 '11 at 00:52