1

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?

0

4 Answers 4

2

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);
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

0

http://www.ibm.com/developerworks/library/x-javaxpathapi.html

HTH

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.