Reputation: 15251
I have a DOM Document
(org.w3c.dom
) but I need to be able to find elements by XPath and such. Which parser or library out there can provide this feature?
Upvotes: 1
Views: 1456
Reputation: 49197
My preferred xpath
library is jaxen
. It's simple to use and powerful. Sample usage (exceptions deferred):
List<Node> matchingNodes = new DOMXPath("//myxpath").selectNodes(document);
Upvotes: 0
Reputation: 691933
http://download.oracle.com/javase/6/docs/api/javax/xml/xpath/package-summary.html
The end of the page even has an example on how to use XPath on a DOM document :
// parse the XML as a W3C Document
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = builder.parse(new File("/widgets.xml"));
XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "/widgets/widget";
Node widgetNode = (Node) xpath.evaluate(expression, document, XPathConstants.NODE);
Upvotes: 2
Reputation: 12349
http://www.ibm.com/developerworks/library/x-javaxpathapi.html
HTH
Upvotes: 0
Reputation: 308131
If you already havea Document
then you don't need a parsers, as the XML is already parsed.
There's the javax.xml.xpath
package that provides XPath functionality.
Upvotes: 2