-1

Is there a good free XPath evaluator source code available?

We have a tree style data structure which is not too dissimilar to XML. However, it would be great to include something similar to an XPath evaluator (lightweight) to navigate through the structure.

Would be cool to XPath expressions such as:

\Node1*\Child20* \Node11 sum(\Node1*\value)

etc etc...

I think it would take a while to implement a solution ourselves but unfortunately we have tight very timescales. We would need to source to enable us to go through our structure based on the tokenized string.

I am not looking for XPath libraries as such, just the code to evaluate XPath style expressions.

I want to be able to interpret an XPath based expression and process our own internal data structure - our own object model implementation.

I have come across JXPath on my travels but have never used it but it seems like something that I could use. Has anyone ever used this?

Thanks,

Andez

pnuts
  • 58,317
  • 11
  • 87
  • 139
Andez
  • 5,588
  • 20
  • 75
  • 116

5 Answers5

1

HTML Cleaner is pretty good, I always use this library for Xpath parser. You can try: http://htmlcleaner.sourceforge.net/

Pete Houston
  • 14,931
  • 6
  • 47
  • 60
1

You will find a complete open source implementation of XPath in Java in Apache Xalan.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
0

Java has XPath support built-in.

The following code extracts the value of an xpath expression:

String xml = ".... your xml goes here ...";
String expression = "... your XPath goes here ...";
DocumentBuilderFactory xmlFact = DocumentBuilderFactory.newInstance();
xmlFact.setNamespaceAware(true);
DocumentBuilder builder = xmlFact.newDocumentBuilder();
InputSource inputSource = new InputSource(new StringReader(xml));
Document doc = builder.parse(inputSource);
XPath xpath = XPathFactory.newInstance().newXPath();
String value = (String) xpath.evaluate(expression, doc, XPathConstants.STRING);
  • Hi. I am not using underlying XML. We have our own data structure (resembling XML type tree), but I want to use the expression based on XPath to navigate our structure and return our custom underlying types - similar to what XPath does. – Andez Oct 06 '11 at 10:14
  • I don't understand what you mean with "resembling XML". If it's well formed, you can apply an XPath expression. –  Oct 06 '11 at 10:19
  • @a_horse_with_no_name: He wants to query a custom object tree, not a XML document. Something that XPath (theoretically) supports. – millimoose Oct 06 '11 at 10:42
0

Saxon's XPath engine will work with any input data structure if you write an adapter for your data structure to implement Saxon's NodeInfo interface. This seems a much cleaner approach than modifying the source code of an XPath processor, which you would then have to maintain for ever.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
0

JXPath should be fine for this. But you will probably have to integrate it with your data structure as described at http://commons.apache.org/jxpath/users-guide.html#Custom_Pointers_and_Iterators.

I did a similar thing recently and it worked quiet nicely. Documentation is a little weak but looking it the existing implementations like DomNodePointer/Dom***Iterator should greatly help with your implementation.

For completeness, Jaxen would allow a similar approach. But due to licensing reasons I couldn't use it myself.

Robert
  • 1
  • 1