Java org.w3c.dom : is there a Java parser library?
I have a DOM Document
(or开发者_运维知识库g.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?
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);
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.
http://www.ibm.com/developerworks/library/x-javaxpathapi.html
HTH
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);
精彩评论