|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Features XPath Support in Oracle JDeveloper 11g
JDeveloper provides an XPath Search GUI tool to select nodes from an XML document using XPath
By: Deepak Vohra
Aug. 18, 2009 07:45 AM
XML documents can be used to transfer data. The data in an XML document can be retrieved either with the JAXP (Java API for XML Processing) With SAX and DOM APIs, node lists have to be iterated to access a particular node. Another advantage of navigating an XML document with XPath is that an attribute node may be selected directly. With DOM and SAX APIs, an element node has to be selected before an element attribute can be selected. Here we'll discuss XPath support in JDeveloper. What Is XPath? Specific nodes including element, attribute, and text nodes can be accessed with XPath. XPath supports nodes in a namespace. Nodes in XPath are selected with an XPath expression. An expression is evaluated to yield an object of one of the following four types: node set, Boolean, number, or string. For an introduction to XPath refer to the W3C Recommendation for XPath. As a brief review, expression evaluation in XPath is performed with respect to a context node. The most commonly used type of expression in XPath is a location path. XPath defines two types of location paths: relative location paths and absolute location paths. A relative location path is defined with respect to a context node and consists of a sequence of one or more location steps separated by "/". A location step consists of an axis, a node test, and predicates. An example of a location step is: child::journal[position()=2] In this example, the child axis contains the child nodes of the context node. Node test is the journal node set, and the predicate is the second node in the journal node set. An absolute location path is defined with respect to the root node, and starts with "/". The difference between a relative location path and an absolute location path is that a relative location path starts with a location step and an absolute location path starts with "/". XPath in Oracle XDK 11g In this article we'll parse an example XML document with the DOMParser class, obtain an XMLDocument object for the XML document, and select nodes from the document with the XMLDocument class select methods. The different select methods in the XMLDocument class are discussed in Table 2. The XML document parsed in this article has a namespace declaration xmlns:journal="http://www.xdk11g.com/xpath" for elements in the namespace with the prefix journal. For an introduction on namespaces in XML refer to the W3C Recommendation on Namespaces in XML 1.0. catalog.xml, the example XML document, is shown in Listing 1. Setting the Environment To develop an application with XPath, add the required libraries to the project classpath. Select the project node in Application Navigator and select Tools | Project Properties. In the Project Properties window, select the Libraries and Classpath node. To add a library, select the Add Library button. Select the Oracle XML Parser v2 library. Click on the OK button in the Project Properties window. We also need to add an XML document that is to be parsed and navigated with XPath. To add an XML document, select File | New. In the New Gallery window, select Categories | General | XML and Items | XML Document. Click on the OK button. In the Create XML File window specify the file name catalog.xml in the File Name field, and click on the OK button. Copy the catalog.xml listing to the catalog.xml file in the Application Navigator. The directory structure of the XPath project is shown in Figure 1. XPath Search If you want to select nodes in a specific namespace or nodes from different namespaces, first add the namespace Prefix and URI to the XPath Search tool using the Add button. Subsequently, include namespace prefixes in the XPath expression. The XPath Search tool is shown in Figure 2. To navigate catalog.xml with XPath, select catalog.xml in the Application Navigator and select Search | XPath Search (see Figure 3). In the following subsections, we'll select example nodes using absolute location paths and relative location paths. Use a relative location path if the XML document is large and a specific node is required. Use a relative path if the node from which subnodes are to be selected and the relative location path are known. Use an absolute location path if the XML document is small, or if the relative location path isn't known. The objective is to use minimum XPath navigation. Use the minimum number nodes to navigate to select the required node. Selecting Nodes with Absolute Location Paths The title elements get selected. Title elements from the journal:article elements in the journal namespace don't get selected because a namespace hasn't been applied to the XPath expression (see Figure 5). As an other example, select the title element in the first article element using the XPath expression /catalog/journal/article[1]/title. We're not using namespaces yet. The XPath expression is specified in the Expression field (see Figure 6). The title of the first article element gets selected as shown in the JDeveloper output (see Figure 7). Attribute nodes can also be selected with XPath. Attributes are selected by using the "@" prefix. As an example, select the section attribute in the first article element in the journal element. The XPath expression for selecting the section attribute is /catalog/journal/article[1]/@section and is specified in the Expression field. Click on the OK button to select the section attribute (see Figure 8). The attribute section gets outputted in JDeveloper: Applying XPath Expression... Selecting Nodes with Relative Location Paths The title of the first article element in the journal element gets selected as shown here: Applying XPath Expression... Selecting Namespace Nodes The title elements in the journal element (in the journal namespace) get selected and outputted in JDeveloper: Applying XPath Expression... Attributes in a namespace can be selected with XPath Search. As an example, select the section attributes in the journal namespace. Specify the XPath expression to select the section attributes in the Expression field and click the OK button (see Figure 11). Section attributes in the journal namespace get selected: Applying XPath Expression... Selecting Nodes with XPath API The XMLDocument class has select methods to select nodes with an XPath expression. A single node can be selected or a NodeList of nodes can be selected. Nodes declared in a namespace may also be selected. First, we need to import the oracle.xml.parser.v2 package that has the XMLDocument class and the parser class DOMParser from which an XMLDocument can be obtained. import oracle.xml.parser.v2.*; Creating the DOM parser DOMParser domParser=new DOMParser(); The DOMParser class that extends the XMLParser class has overloaded the parse methods to parse an XML document from different input sources. An XML document can be parsed from InputSource, InputStream, Reader, String, or URL. In the XPathParser.java example application we'll parse an example document from a FileReader. Create a FileReader object from a File object and parse it with the parse(Reader) method as shown in the following listing: domParser.parse(new FileReader(xmlDocument)); The variable xmlDocument is the File representation of the XML document catalog.xml. The class that provides XPath functionality is XMLDocument. Obtain an XMLDocument object from the DOMParser object with the getDocument() method. XMLDocument document=domParser.getDocument(); Method selectSingleNode(String) XMLNode titleNode=(XMLNode)document.selectSingleNode("/catalog/ The title element value can be selected by first selecting the text node in the title element node using the getFirstChild() method and subsequently selecting the text node value using the getNodeValue() method, as shown in the following listing: String title=titleNode.getFirstChild().getNodeValue(); As another example of the selectSingleNode(String) method, select the author of the article with the title Oracle Database 11g Redux, as shown here: XMLNode authorNode=(XMLNode)document.selectSingleNode("/catalog/ The author element text value is outputted by first selecting the text node in the author element node using the getFirstChild() method and subsequently selecting the text node value using the getNodeValue() method, as shown here: String author=authorNode.getFirstChild().getNodeValue(); The XMLDocument class selectSingleNode(String) can also be used to select an attribute node. As an example, select the section attribute of the second article in the journal date March-April 2008: XMLNode sectionNode=(XMLNode)document.selectSingleNode("/catalog/ The section node value can be selected with the getNodeValue() method: String section=sectionNode.getNodeValue(); Method selectNodes(String) NodeList nodeList=document.selectNodes("/catalog/journal[@date='March- The method selectNodes(String) returns a NodeList that can be iterated over to output title elements: for(int i=0; i<nodeList.getLength(); i++){ The method selectNodes(String) can also be used to select attribute nodes. For example, select all the section attributes for the articles in the journal of date March-April 2008: NodeList The NodeList of section nodes can be iterated over to output section node values as shown in the following listing: for(int i=0; i<nodeList.getLength(); i++){ Method selectSingleNode(String,NSResolver) To select nodes in a namespace, create a class - for example, CustomNSResolver - that implements the NSResolver interface. In the implementation class, implement the NSResolver method resolveNamespacePrefix(String prefix) as shown in the following listing: class CustomNSResolver implements NSResolver{ The resolveNamespacePrefix method accepts a prefix and returns a URI. Create an NSResolver object. Set the namespace prefix to resolve using the resolveNamespacePrefix method: CustomNSResolver resolver=new CustomNSResolver(); In the XML document example, catalog.xml, one of the journal elements is in the journal namespace. As an example, select the section attribute in the journal namespace in the first article element (in journal namespace) in the journal element (in journal namespace). The section attribute value will be outputted with the getNodeValue() method, as shown in the following listing: XMLNode sectionNode= (XMLNode)document.selectSingleNode("/catalog/ As a contrast between the selectSingleNode method that takes an NSResolver and the selectSingleNode method that doesn't, the method that doesn't take an NSResolver doesn't select nodes in a namespace. On the other hand, the one that does selects nodes in a namespace. If the method that doesn't take an NSResolver is invoked with an XPath expression containing namespace nodes, the following error is generated: XSLException Namespace prefix ‘journal' used but not declared. Method selectNodes(String,NSResolver) NodeList nodeList=document.selectNodes("/catalog/journal:journal[@ The namespace prefix journal in the XPath expression is resolved with an NSResolver object. The NodeList returned by the selectNodes(String, NSResolver) method can be iterated over to output the title elements. for(int i=0; i<nodeList.getLength(); i++){ Running the Java Application 1. First we import the required packages from XDK 11g. import oracle.xml.parser.v2.*; 2. Next we add the XPathParser Java class declaration. public class XPathParser{ 3. We define the parseDocument method to parse an XML document with the DOMParser public void parseDocument(File xmlDocument){ 4. Next we create a DOMParser object and parse the XML document. DOMParser domParser=new DOMParser(); 5. We specify the NSResolver object to resolve namespaces. CustomResolver resolver=new CustomResolver(); 6. Next we select nodes using the selectSingleNode(String) method as shown in Listing 2 below. sectionNode=(XMLNode)document.selectSingleNode("/catalog/journal: 9. We also select nodes using the selectNodes(String,NS Resolver) method as shown in Listing 4 below. class CustomResolver implements NSResolver{ 11. Finally, we define the main method in which we create an instance of the XPathParser class and invoke the parseDocument method. public static void main(String[] argv){ To run the XPathParser.java application in JDeveloper, right-click on the XPathParser.java node in the Application Navigator and select Run. The output from the XPathParser.java application is shown in Figure 12. Summary Reader Feedback: Page 1 of 1
Latest Cloud Developer Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week
Breaking Cloud Computing News
|
|||||||||||||||||||||||||||||||||||||||||||||||||