Java APIs for XML

JAX provides standard interfaces for parsing, transforming, and otherwise manipulating XML content. The latest versions of these APIs are complete enough so that many applications need not worry about the underlying implementation.

However, for some XML- intensive applications, JAX isn't enough. The applications must instead rely on additional features or behavior provided by specific JAX implementations like Crimson or Xerces. Unfortunately, the bundling of JAX within J2EE containers, and more recently, within the Java Runtime Environment itself has made life difficult for developers who rely on a particular version. Since a much higher classloader loads the standard Java APIs and the container, it can be difficult or impossible to override their JAX implementation for a single application.

Determining Your JAX Version

Often, the first step to solving a JAX conflict is simply to understand which libraries are being loaded. If you are using Java 1.4, this will default to the Crimson implementation included in $JAVA_HOME/jre/lib/rt.jar . If you are using a previous version of Java that did not include a JAX implementation, then it will default to the Crimson implementation included in OC4J in $J2EE_HOME/lib/crimson.jar .

Tip 

To get the exact version number of the bundled Crimson implementation, look at META-INF/Manifest.mf within the crimson.jar library.

The Crimson libraries are not as actively maintained as the popular Xerces implementation, so it's possible that either the Java Runtime Environment (JRE) or OC4J or both may use Xerces in the future. To determine which implementation is bundled with your JRE, look for org.apache.crimson or org.apache.xerces classes within rt.jar . To determine which implementation is bundled with OC4J, look for crimson.jar or xerces.jar within $J2EE_HOME/lib .

Overriding the JAX Implementation

Ideally, your application will be able to use the Crimson libraries bundled with OC4J or the JRE. However, if you rely on specific features of Xerces or another JAX implementation, then use the following steps to override the default implementation. These steps assume that you're using the JAXP method for obtaining a parser implementation. If you're directly instantiating the parser of your choice then you can ignore the following:

  • In JDK 1.3.x, remove crimson.jar from $J2EE_HOME/lib and replace it with your preferred JAX implementation.

  • In JDK 1.4.x, place your preferred JAX implementation library in a directory on the local file system. When starting OC4J, set the java.endorsed.dirs system property to point to that location, like this:

     java -Djava.endorsed.dirs=/my/xml/dir ... 

If you wish to configure the implementation programmatically by setting the following system properties, then you can ignore the following:

  • javax.xml.parsers.SAXParserFactory . Use this property to specify the full name of the SAXParserFactory class implementation.

  • javax.xml.parsers.DocumentBuilderFactory . Use this property to specify the full name of DocumentBuilderFactory implementation class.

Testing the JAX Override

It's quite simple to check which XML implementation is being used. This Servlet application will display the name of the XML document implementation loaded using JAXP, as follows :

 package com.apress.oracle10g.xml; import java.io.IOException; import java.io.InputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; public class JaxServlet extends HttpServlet {     public void doGet(HttpServletRequest request, HttpServletResponse response)             throws ServletException, IOException {         try {             InputStream webXml = this.getServletContext().getResourceAsStream(                     "/WEB-INF/web.xml");             DocumentBuilder builder = DocumentBuilderFactory.newInstance()                     .newDocumentBuilder();             Document doc = builder.parse(webXml);             response.setContentType("text/html");             response.getOutputStream().println(doc.getClass().getName());         } catch (Exception ex) {             throw new ServletException(ex);         }     } } 

This code is fairly simple. It loads the application's web.xml file using the JAXP-supplied parser. Then it simply outputs the name of the class, implementing the Document interface, which was supplied by JAXP. You'll be able to tell the implementations that are being used from the package name displayed.



Oracle Application Server 10g. J2EE Deployment and Administration
Oracle Application Server 10g: J2EE Deployment and Administration
ISBN: 1590592352
EAN: 2147483647
Year: 2004
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net