The getter methods in the
org.w3c.dompackage API are commonly used to parse an XML document. But J2SE 5.0 also provides thejavax.xml.xpathpackage to parse an XML document with the XML Path Language (XPath) . The JDOMorg.jdom.xpath.XPathclass also has methods to select XML document node(s) with an XPath expression, which consists of a location path of an XML document node or a list of nodes.Parsing an XML document with an XPath expression is more efficient than the getter methods, because with XPath expressions, an
Elementnode may be selected without iterating over a node list. Node lists retrieved with the getter methods have to be iterated over to retrieve the value of element nodes. For example, the secondarticlenode in thejournalnode in the example XML document in this tutorial (listed in the Overview section below) may be retrieved with the XPath expression:Element article=(Element) (xPath.evaluate("/catalog/journal/article[2]/title", inputSource, XPathConstants.NODE));In the code snippet,
xPathis anjavax.xml.xpath.XPathclass object, andinputSourceis anInputSourceobject for an XML document. With theorg.w3c.dompackage getter methods, the secondarticlenode in thejournalnode is retrieved with the code snippet:Document document; NodeList nodeList=document.getElementsByTagName("journal"); Element journal=(Element)(nodeList.item(0)); NodeList nodeList2=journal.getElementsByTagName("article"); Element article=(Element)nodeList2.item(1);Also, with an XPath expression, an
Attributenode may be selected directly, in comparison to the getter methods, in which anElementnode is required to be evaluated before anAttributenode is evaluated. For example, the value of thelevelattribute for thearticlenode with thedateJanuary-2004is retrieved with an XPath expression:String level = xPath.evaluate("/catalog/journal/article[@date='January-2004']/@level", inputSource);By comparison, the
org.w3c.dompackage makes you retrieve theorg.w3c.dom.Elementobject for thearticle, and then get itslevelattribute with:String level=article.getAttribute("level");Overview
In this tutorial, an example XML document is parsed with J2SE 5.0's
XPathclass and JDOM'sXPathclass. XML document nodes are selected with XPath expressions. Depending on the XPath expression evaluated, the nodes selected are eitherorg.w3c.dom.Elementnodes ororg.w3c.dom.Attributenodes. The example XML document, catalog.xml, is listed below:<?xml version="1.0" encoding="UTF-8"?> <catalog xmlns:journal="http://www.w3.org/2001/XMLSchema-Instance" > <journal:journal title="XML" publisher="IBM developerWorks"> <article journal:level="Intermediate" date="February-2003"> <title>Design XML Schemas Using UML</title> <author>Ayesha Malik</author> </article> </journal:journal> <journal title="Java Technology" publisher="IBM developerWorks"> <article level="Advanced" date="January-2004"> <title>Design service-oriented architecture frameworks with J2EE technology</title> <author>Naveen Balani </author> </article> <article level="Advanced" date="October-2003"> <title>Advance DAO Programming</title> <author>Sean Sullivan </author> </article> </journal> </catalog>The example XML document has a namespace declaration,
xmlns:journal="http://www.w3.org/2001/XMLSchema-instance", for elements in thejournalprefix namespace.This article is structured into the following sections:
- Preliminary Setup
- Parsing with the JDK 5.0 XPath Class
- Parsing with the JDOM XPath Class
Preliminary Setup
To use J2SE 5.0's XPath support, the
javax.xml.xpathpackage needs to be in theCLASSPATH. Install the new version of the J2SE 5.0 SDK. To parse an XML document with the JDK 5.0 XPath class, add the<JDK5.0>\jre\lib\rt.jar file to theCLASSPATHvariable, if it's not already in theCLASSPATH.<JDK5.0>is the directory in which JDK 5.0 is installed.The
org.apache.xpath.NodeSetclass is required in theCLASSPATH. Install Xalan-Java; extract xalan-j-current-bin.jar to a directory. Add<Xalan>/bin/xalan.jar to theCLASSPATH, where<Xalan>is the directory in which Xalan-Java is installed.To parse an XML document with the JDOM
XPathclass, the JDOM API classes need to be in theCLASSPATH. Install JDOM; extract the jdom-b9.zip file to an installation directory. Add<JDOM>/jdom-b9/build/jdom.jar,<JDOM>/jdom-b9/lib/saxpath.jar,<JDOM>/jdom-b9/lib/jaxen-core.jar,<JDOM>/jdom-b9/lib/jaxen-jdom.jar, and<JDOM>/jdom-b9/lib/xerces.jar to theCLASSPATHvariable, where<JDOM>is the directory in which JDOM is installed.Parsing with the JDK 5.0 XPath Class
The
javax.xml.xpathpackage in J2SE 5.0 has classes and interfaces to parse an XML document with XPath. Some of the classes and interfaces in JDK 5.0 are listed in the following table:
Class/Interface Description XPath(interface)Provides access to the XPath evaluation environment. Provides the evaluatemethods to evaluate XPath expressions in an XML document.XPathExpression(interface)Provides the evaluatemethods to evaluate compiled XPath expressions in an XML document.XpathFactory(class)Used to create an XPath object. In this section, the example XML document is evaluated with the
javax.xml.xpath.XPathclass. First, import thejavax.xml.xpathpackage.import javax.xml.xpath.*;The
evaluatemethods in theXPathandXPathExpressioninterfaces are used to parse an XML document with XPath expressions. TheXPathFactoryclass is used to create anXPathobject. Create anXPathFactoryobject with the staticnewInstancemethod of theXPathFactoryclass.XPathFactory factory=XPathFactory.newInstance();Create an
XPathobject from theXPathFactoryobject with thenewXPathmethod.XPath xPath=factory.newXPath();Create and compile an XPath expression with the
compilemethod of theXPathobject. As an example, select thetitleof the article with itsdateattribute set toJanuary-2004. An attribute in an XPath expression is specified with an@symbol. For further reference on XPath expressions, see the XPath specification for examples on creating an XPath expression.XPathExpression xPathExpression= xPath.compile("/catalog/journal/article[@date='January-2004']/title");Create an
InputSourcefor the example XML document. AnInputSourceis a input class for an XML entity. Theevaluatemethod of theXPathExpressioninterface evaluates either anInputSourceor a node/node list of the typesorg.w3c.dom.Node,org.w3c.dom.NodeList, ororg.w3c.dom.Document.InputSource inputSource = new InputSource(new FileInputStream(xmlDocument)));
xmlDocumentis thejava.io.File objectof the example XML document.File xmlDocument = new File("c:/catalog/catalog.xml");Evaluate the XPath expression with the
InputSourceof the example XML document to evaluate over.String title = xPathExpression.evaluate(inputSource);The result of the XPath expression evaluation is the title:
Design service-oriented architecture frameworks with J2EE technology.TheXPathobject may be directly evaluated to evaluate the value of an XPath expression in an XML document without first compiling an XPath expression. Create anInputSource.inputSource = new InputSource(new FileInputStream(xmlDocument)));As an example, evaluate the value of the
publishernode in thejournalelement.String publisher = xPath.evaluate("/catalog/journal/@publisher", inputSource);The result of the
XPathobject evaluation is the attribute value:IBM developerWorks. Theevaluatemethod in theXPathclass may also be used to evaluate a node set. For example, select the node or set of nodes that correspond to thearticleelement nodes in the XML document. Create the XPath expression that represents a node set.String expression="/catalog/journal/article";Select the node set of
articleelement nodes in the example XML document with theevaluatemethod of theXPathobject.NodeSet nodes = (NodeSet) xPath.evaluate(expression, inputSource, XPathConstants.NODESET);
XpathConstants.NODESETspecifies the return type of theevaluatemethod as aNodeSet. The return type may also be set toNODE,STRING,BOOLEANorNUMBER. TheNodeSetclass implements theNodeListinterface. To parse the nodes in the node set, cast theNodeSetobject toNodeList.NodeList nodeList=(NodeList)nodes;Thus, nodes in an XML document get selected and evaluated without iterating over the getter methods of the
org.w3c.domAPI. The example programXPathEvaluator.javais used to parse an XML document with the JDK 5.0XPathclass.Parsing with the JDOM XPath Class
The JDOM API
XPathclass supports XPath expression to select nodes from an XML document. Some of the methods in the JDOMXPathclass are illustrated in the following table:
XPath Class Method Description selectSingleNodeUsed to select a single node that matches an XPath expression. selectNodesUsed to select a list of nodes that match an XPath expression. addNamespaceUsed to add a namespace to match an XPath expression with namespace prefixes. In this section, the procedure to select nodes from the example XML document catalog.xml with the JDOM
XPathclass shall be discussed. The node/nodes selected by theselectmethods are modified, and the modified document is output to an XML document. First, import the JDOMorg.jdom.xpathpackage classes.import org.jdom.xpath.*;Create a
SAXBuilder.SAXBuilder saxBuilder = new SAXBuilder("org.apache.xerces.parsers.SAXParser");Parse the XML document catalog.xml with the
SAXBuilder.org.jdom.Document jdomDocument = saxBuilder.build(xmlDocument);
xmlDocumentis thejava.io.Filerepresentation of the XML document catalog.xml. The static methodselectSingleNode(java.lang.Object context, String XPathExpression)selects a single node specified by an XPath expression. If more than one nodes match the XPath expression, the first node that matches the XPath expression gets selected. Select the attribute nodelevelof an elementarticlein ajournalwithtitleset toJava Technology, and witharticleattributedateset toJanuary-2004, with an XPath expression.org.jdom.Attribute levelNode = (org.jdom.Attribute)(XPath.selectSingleNode( jdomDocument, "/catalog//journal[@title='JavaTechnology']" + "//article[@date='January-2004']/@level"));The
levelattribute valueAdvancedgets selected. Modify thelevelnode.levelNode.setValue("Intermediate");The
selectSingleNodemethod may also be used to select an element node in an XML document. As an example, select atitlenode. Select thetitlenode with an XPath expression.org.jdom.Element titleNode = (org.jdom.Element) XPath.selectSingleNode( jdomDocument, "/catalog//journal//article[@date='January-2004']/title");The
titlenode with valueDesign service-oriented architecture frameworks with J2EE technologygets selected. Modify thetitlenode.titleNode.setText( "Service Oriented Architecture Frameworks");The static method
selectNodes(java.lang.Object context, String XPathExpression)selects all of the nodes specified by an XPath expression. Select all of thearticlenodes for thejournalwith atitleset toJava Technology.java.util.List nodeList = XPath.selectNodes(jdomDocument, "/catalog//journal[@title='Java Technology']//article");Modify the
articlenodes. Add an attribute to thearticlenodes.Iterator iter=nodeList.iterator(); while(iter.hasNext()) { org.jdom.Element element = (org.jdom.Element) iter.next(); element.setAttribute("section", "Java Technology"); }The JDOM
XPathclass supports selection of nodes with namespace prefixes. To select a node with a namespace, add a namespace to an XPath:XPath xpath = XPath.newInstance( "/catalog//journal:journal//article/@journal:level"); xpath.addNamespace("journal", "http://www.w3.org/2001/XMLSchema-Instance" );A namespace with the prefix
journalgets added to theXPathobject. Select a node with a namespace prefix:levelNode = (org.jdom.Attribute) xpath.selectSingleNode(jdomDocument);The attribute node
journal:levelgets selected. Modify thejournal:levelnode.levelNode.setValue("Advanced");The Java program JDomParser.java is used to select nodes from the catalog.xml XML document. In this section, the procedure to select nodes from an XML document with the JDOM
XPathclassselectmethods was explained. The nodes selected are modified. The modified document is output to a XML document with theXMLOutputterclass. catalog-modified.xml is the output XML document.Conclusion
In this tutorial, an XML document was parsed with XPath. XPath is used only to select nodes. XPath APIs discussed in this tutorial do not have the provision to set values for XML document nodes with XPath. To set values for nodes, the setter methods of the
org.w3c.dompackage are required.Resources
- XPath specification
- JDK 5.0 XPath package
- JDOM
XPathclass- Example code for this article: xpath-java.zip
Return to ONJava.com
浙公网安备 33010602011771号