Good Free Java XPath Evaluator source - JXPath perhaps?
Is there a good free XPath evaluator so开发者_JAVA技巧urce 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
HTML Cleaner
is pretty good, I always use this library for Xpath parser. You can try: http://htmlcleaner.sourceforge.net/
You will find a complete open source implementation of XPath in Java in Apache Xalan.
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);
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.
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.
精彩评论