Show TOC Start of Content Area

Process documentation Obtaining SAX and DOM Parsers Locate the document in its SAP Library structure

Purpose

To parse an XML file, you have to access and use SAX or DOM parsers, or Transformer-s. This topic describes how you do this.

Process Flow

To obtain a SAXParser, the user must first get its corresponding SAXParserFactory through the static SAXParserFactory.newInstance() method and then the SAXParser itself by the factory newSAXParser()method.

Similarly, DocumentBuilder-s (DOM parsers) and their factories are obtained through DocumentBuilderFactory, and Transformer-s (XSLT transformers) are obtained through TransformerFactory. Every factory's newInstance() method uses a specific algorithm for finding the JAXP implementation. Since JAXP 1.1.3 (also part of JDK 1.4), the factory find algorithm is the following:

·        Searches for a system property named after the appropriate factory

¡        javax.xml.parsers.DocumentBuilderFactory

¡        javax.xml.parsers.SAXParsersFactory

¡        javax.xml.transform.TransformerFactory

·        If no property is found, then the jaxp.properties file of the installed JDK is searched for properties having such names.

·        If this file is not found or it does not contain a property for the search factory, then the actual search logic follows (used also in the J2EE Engine):

¡        Get a class loader by invoking Thread.getCurrentThread().getContextClassLoader()

¡        Use this classloader to load a resource using Classloader.getResourceAsStream(factoryResource), where factoryResource is a file named after the properties above and located in META-INF/services/, for example META-INF/services/javax.xml.parsers.DocumentBuilderFactory

¡        In case this resource is found, it is loaded and the contents of this file is a string specifying the class for the JAXP factory implementation

¡        Then this factory is loaded using the same class loader

·        In case no such file is found, a fallback value is to be loaded. This fallback value is specific for the JDK or the JAXP interfaces provider

For example, a SAXParser can be obtained through:

This graphic is explained in the accompanying text

ClassLoader oldLoader = null;

SAXParserFactory fact;

try {

   oldLoader = Thread.currentThread().getContextClassLoader();

   Thread.currentThread().setContextClassLoader(

     this.getClass().getClassLoader());

   fact = SAXParserFactory.newInstance();

} finally {

   Thread.currentThread().setContextClassLoader(oldLoader);

}

Caution

It is essential that you set back the class loader that originally was set. Some other applications may rely on it. Problems may occur if these applications find a class loader that has the JAXP implementation in it.

This means that the component to which this class belongs has a reference to the SAP XML Toolkit library. Providing the classloader will implicitly mean that this loader can load the SAP XML Toolkit classes. In case this is not done, or it is done but the component does not have a reference to the SAP XML Toolkit, the Standard JDK 1.4 parser will be loaded – Crimson for Sun’s JDK, Xerces for IBM’s.

For J2EE applications, the Context classloader is already preset by the Thread Manager. This is not possible for services, libraries, and resource adapters as they can be invoked also by other components that may have preset the context classloader.

Note

Setting system properties in a system that supports more than one parser is prohibited.

The JAXP view of J2EE Engine XML tools is placed in the com.sap.engine.lib.jaxp package. The currently supported version is JAXP 1.2.

 

End of Content Area