KJW
KJW

Reputation: 15251

Java org.w3c.dom : is there a Java parser library?

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

Answers (4)

Johan Sjöberg
Johan Sjöberg

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

JB Nizet
JB Nizet

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

Joachim Sauer
Joachim Sauer

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

Related Questions