Using the XML Parser

Including the Oracle XML parser in your Java programs allows you to write applications that can search and process XML documents. The XML parser is written in Java, and facilitates processing an XML document as either:

OR

The parser also checks whether an XML document is well-formed, and optionally, if it is valid. It also contains an integrated XSLT processor for transforming XML documents.

In the samples\xmlsamples directory, the JDeveloper project JDevXMLExamples.jpr contains several Java programs that demonstrate some of the ways in which you can use the XML parser. You can open the project in JDeveloper and inspect the individual files. You can also run the files from the Navigation pane:

  1. Open the project in JDeveloper: samples\xmlsamples\JDevXMLExamples.jpr.

  2. From the JDeveloper menu bar, select Project | Project Properties, open the Paths tab, and ensure that these libraries are present in the Java libraries list box:

If they are not present, click Add and select the libraries from the Select a Java Library dialog box.

  1. To run individual .java files, right-click them in the Navigation pane and select Run.

Files in the Example XML Project

This section provides a brief description of the files in the example XML project JDevXMLExamples.jpr.

XMLForQueryResults.java

The XMLForQueryResults.java program uses the ConnectionHelper.java program to connect to the database. The XMLForQueryResults.java program performs a query and generates XML query results using the OracleXMLQuery class from the Oracle XML SQL Utility for Java.

TransformedShoppingCart.java

The TransformedShoppingCart.java program loads Sample.xml file from an input stream and uses the XML parser and the selectNodes() method to search for particular nodes. The program prints the results, using the style sheet Sample.xsl to determine the output format.

To run the program successfully, you must copy Sample.xml and Sample.xsl to the project output path JDeveloper_Home\myclasses, where JDeveloper_Home represents the JDeveloper home directory.

ParseXMLfromURL.java

The ParseXMLfromURL.java program uses the XML parser to parse an XML file from a given URL passed as the first argument on the command line. The program prints the results. If the program cannot parse the file successfully, it prints an error. The program also checks that the format of the given URL is valid.

To run this program from JDeveloper, select Project | Project Properties from the menu bar, and enter the argument name in the Run/Debug tab of the Properties dialog.

ParseXMLfromString.java

The ParseXMLfromString.java program uses the XML parser to parse an XML file from a string and prints the results. If the parser encounters an error, the program prints the line and column number where it occurred, the offending expression, and an error message.

FindElement.java

The FindElement.java program uses the XML parser and an XPath expression to search the Sample.xml file for specific nodes. The XPath expression specified in FindElement.java locates all Item elements at any level of the document which have a Qty child whose value is greater than zero, and a Discount child whose value greater than four.

To run the program successfully, you must copy Sample.xml and Sample.xsl to the project output path JDeveloper_Home\myclasses, where JDeveloper_Home represents the JDeveloper home directory.

ConnectionHelper.java

The ConnectionHelper.java program selects the appropriate connection for the user name/password SCOTT/TIGER based on whether the program is running inside the Oracle8i database. If the program is running inside the database it chooses the Server (KPRB) driver. If not, it chooses the JDBC Thin driver.

Sample.xsl

The Sample.xsl file contains a sample XSL style sheet that is used by several programs in this project. It provides the structure and format for printing the contents of a "shopping cart" used in a very simple e-commerce paradigm.

Sample.xml

The Sample.xml file contains sample data in XML format that is used by several programs in this project. The file reflects a very simple e-commerce "shopping cart" paradigm and contains the titles, item numbers, quantities, and price discounts of fictitious books.

XSLT.java

The XSLT.java file contains helper routines to select nodes from XML files, using Oracle's XSLT engine. The helper routines use the built-in selectNodes() methods supported by oracle.xml.parser.v2.XMLNode interface.