I’m using Java 1.6. Given a org.w3c.dom.Node object, how do I get a String XPath expression for where that node falls in an XML document? It is NOT a given that this node or any of its ancestors/descendants have an id attribute.
I’m using Java 1.6. Given a org.w3c.dom.Node object, how do I get a String
Share
I don’t believe there is any easy way via DOM. The problem is that there is not a single canonical way to identify a given node.
Having said that, you might find the the function getXPath from diffxml useful: http://diffxml.cvs.sourceforge.net/viewvc/diffxml/diffxml/src/java/org/diffxml/diffxml/fmes/NodeOps.java?view=markup
It will return an XPath of the form /node()[4]/node()[2] etc.
UPDATE: As diffxml is GPL licensed and getXPath takes a bit of extracting, I released it as XPathGen on github: https://github.com/amouat/XPathGen under an Apache licence.