3 Developing XML Applications with WebLogic Server

This chapter describes how to use the Java programming language and the WebLogic Server XML subsystem to develop XML applications. It is assumed that you know how to use Java Servlets and Java Server Pages (JSPs) to write Java applications.

This chapter includes the following sections:

For information about how to write servlet and JSP applications, see Developing Web Applications, Servlets, and JSPs for Oracle WebLogic Server.

Developing XML Applications: Main Steps

Programmers using the WebLogic Server XML subsystem typically perform some or all of the following programming tasks when developing XML applications:

  1. Parse an XML document.

    The XML document can originate from a number of sources. For example, a programmer might develop a servlet to receive an XML document from a client, write an EJB to receive an XML document from a Servlet or another EJB, and so on. In each instance, the XML document may have to be parsed so that its data can be manipulated.

    See Parsing XML Documents.

  2. Generate a new XML document.

    After a servlet or EJB has received and parsed an XML document and possibly manipulated the data in some way, the Servlet or EJB might need to generate a new XML document to send back to the client or to pass on to another EJB.

    See Generating New XML Documents.

  3. Transform XML data into another format.

    After parsing an XML document or generating a new one, the Servlet or EJB may need to transform it into another format, such as HTML, WML, or plain text.

    See Using JAXP to Transform XML Data.

Parsing XML Documents

This section describes how to parse XML documents using JAXP in both DOM and SAX mode and how to parse XML documents from a servlet.

Note:

For detailed instructions on using the Streaming API for XML (StAX) to parse XML documents, see Using the Streaming API for XML (StAX).

You use the WebLogic Server Administration Console XML Registry to configure the following:

  • Per-document-type parsers, which supersede the default parser for the specified document type.

  • External entity resolution, or the process that an XML parser goes through when requested to find an external file in the course of parsing an XML document

For detailed information on how to use the WebLogic Server Administration Console for these tasks, refer to Administering WebLogic Server XML.

Parsing XML Documents Using JAXP in SAX Mode

The following code example shows how to configure a SAX parser factory to create a validating parser. The example also shows how to register the MyHandler class with the parser. The MyHandler class can override any method of the DefaultHandler class to provide custom behavior for SAX parsing events or errors.

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

...
MyHandler handler = new MyHandler(); 
// MyHandler extends org.xml.sax.helpers.DefaultHandler.

  //Obtain an instance of SAXParserFactory.
  SAXParserFactory spf = SAXParserFactory.newInstance();
  //Specify a validating parser.
  spf.setValidating(true); // Requires loading the DTD.
  //Obtain an instance of a SAX parser from the factory.
  SAXParser sp = spf.newSAXParser();
  //Parse the documnt.
  sp.parse("http://server/file.xml", handler);
...

Note:

If you want to use a parser other than the default parser, you must use the WebLogic Server Administration Console to specify the parser in the XML Registry; otherwise the SaxParserFactory.newInstance method returns the default parser. For instructions about configuring WebLogic Server to use a parser other than the default parser, see Configuring a Parser or Transformer Other Than the Default.

Parsing XML Documents Using JAXP in DOM Mode

The following code example shows how to parse an XML document and create an org.w3c.dom.Document tree from a DocumentBuilder object:

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

...
//Obtain an instance of DocumentBuilderFactory.
DocumentBuilderFactory dbf =
                 DocumentBuilderFactory.newInstance();
//Specify a validating parser.
dbf.setValidating(true); // Requires loading the DTD.
//Obtain an instance of a DocumentBuilder from the factory.
DocumentBuilder db = dbf.newDocumentBuilder();
//Parse the document.
Document doc = db.parse(inputFile);
...

Note:

If you want to use a parser other than the default parser, you must use the WebLogic Server Administration Console to specify it; otherwise the DocumentBuilderFactory.newInstance method returns the default parser. For instructions about configuring WebLogic Server to use a parser other than the default parser, see Configuring a Parser or Transformer Other Than the Default.

Parsing XML Documents in a Servlet

Support for the setAttribute and getAttribute methods was added to version 2.2 of the Java Servlet Specification. Attributes are objects associated with a request. The request object encapsulates all information from the client request. In the HTTP protocol, this information is transmitted from the client to the server by the HTTP headers and message body of the request.

With WebLogic Server, you can use the setAttribute and getAttribute methods to parse XML documents. Use the setAttribute method for SAX mode parsing and the getAttribute method for DOM mode parsing, as described in Using the org.xml.sax.DefaultHandler Attribute to Parse a Document and Using the org.w3c.dom.Document Attribute to Parse a Document.

Before you can use the setAttribute and getAttribute methods, however, you must configure a WebLogic Server servlet filter called weblogic.servlet.XMLParsingHelper (deployed by default on all WebLogic Server instances) as part of your Web application. Configure the servlet filter by adding the following elements to the web.xml deployment descriptor, located in the WEB-INF directory of your Web application:

<filter>
  <filter-name>XMLParsingHelper</filter-name>
  <filter-class>weblogic.servlet.XMLParsingHelper</filter-class>
</filter>

<filter-mapping>
  <filter-name>XMLParsingHelper</filter-name>
  <url-pattern>/*</url-pattern>
  <dispatcher>REQUEST</dispatcher>
</filter-mapping>

For more information on servlet filters, see Filters in Developing Web Applications, Servlets, and JSPs for Oracle WebLogic Server.

Using the org.xml.sax.DefaultHandler Attribute to Parse a Document

The following code example shows how to use the setAttribute method:

import weblogic.servlet.XMLProcessingException;
import org.xml.sax.helpers.DefaultHandler;
...
public void doPost(HttpServletRequest request,
                             HttpServletResponse response)
   throws ServletException, IOException  {
   try {
      request.setAttribute("org.xml.sax.helpers.DefaultHandler",
                           new DefaultHandler());
    } catch(XMLProcessingException xpe) {
      System.out.println("Error in processing XML");
      xpe.printStackTrace();
      return;
    } 
... 

You can also use the org.xml.sax.HandlerBase attribute to parse an XML document, although it is deprecated:

request.setAttribute("org.xml.sax.HandlerBase", 
                      new HandlerBase());

Note:

This code example shows a simple way to parse a document using SAX and the setAttribute method. This method of parsing a document is a WebLogic Server convenience feature, and it is not supported by other servlet vendors. Therefore, if you plan to run your application on other servlet platforms, do not use this feature.

Using the org.w3c.dom.Document Attribute to Parse a Document

The following code example shows how to use the getAttribute method.

import org.w3c.dom.Document;
import weblogic.servlet.XMLProcessingException;

...

public void doPost(HttpServletRequest request,
                            HttpServletResponse response)
throws ServletException, IOException  {

try {
   Document doc = request.getAttribute("org.w3c.dom.Document");
 } catch(XMLProcessingException xpe) {
   System.out.println("Error in processing XML");
   xpe.printStackTrace();
   return;
 }
...

Note:

This code example shows a simple way to parse a document using DOM and the getAttribute method. This method of parsing a document is a WebLogic Server convenience feature, and it is not supported by other servlet vendors. Therefore, if you plan to run your application on other servlet platforms, do not use this feature.

Validating and Non-Validating Parsers

As previously discussed, a well-formed document is one that is syntactically correct according to the rules outlined in the W3C Recommendation for XML 1.0. A valid document is one that follows the constraints specified by its DTD or schema.

A non-validating parser verifies that a document is well-formed, but does not verify that it is valid. To turn on validation while parsing a document (assuming you are using a validating parser), you must:

  • Set the SAXParserFactory.setValidating() method to true, as shown in the following example:

    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true);
    
  • Ensure that the XML document you are parsing includes (either in-line or by reference) a DTD or a schema.

Handling Entity Resolution While Parsing an XML Document

This section provides general information about external entities; how they are identified and resolved by an XML parser; and the features provided by WebLogic Server to improve the performance of external entity resolution in your XML applications.

General Information About External Entities

External entities are chunks of text that are not literally part of an XML document, but are referenced inside the XML document. The actual text might reside anywhere - in another file on the same computer or even somewhere on the Web. While parsing a document, if the parser encounters an external entity reference, it fetches the referenced chunk of text, places the text into the XML document, then continues parsing. An example of an external entity is a DTD; rather than including the full text of the DTD in the XML document, the XML document has a reference to the DTD that is stored in a separate file.

There are two ways to identify an external entity: a system identifier and a public identifier. System identifiers use URIs to reference an external entity based on its location. Public identifiers use a publicly declared name to refer the information.

The following example shows how a public identifier is used to reference the DTD for the application.xml file that describes a Java EE application archive (*.ear file):

<!DOCTYPE application PUBLIC "-//Sun Microsystems, 
Inc.//DTD J2EE Application 1.2//EN">

The following example shows a reference to an external DTD by a system identifier only:

<!DOCTYPE application SYSTEM "http://java.sun.com/j2ee/dtds/application_1_2.dtd">

Here is a reference that uses both the public and system identifier; note that the keyword SYSTEM is omitted:

<!DOCTYPE application 
PUBLIC "-//Sun Microsystems, Inc.//DTD J2EE Application 1.2//EN"
"http://java.sun.com/j2ee/dtds/application_1_2.dtd">

Using the WebLogic Server Entity Resolution Features

Use the following WebLogic Server features to improve the performance of external entity resolution in your XML applications:

  • Permanently store a copy of an external entity on the computer that hosts the WebLogic Administration Server.

  • Specify that WebLogic Server automatically retrieve and cache an external entity that resides in an external repository that supports an HTTP interface, such as a URL. You can specify that WebLogic Server cache the entity either in memory or on disk and specify when the cached entry becomes stale, at which point WebLogic Server automatically updates the cached entry.

    Using the retrieve-and-cache feature, you do not have to actually copy the external entity to the local computer. The XML application refers to the actual external entity only at specified time intervals, rather than each time the document is parsed, thus potentially greatly improving the performance of your application while also keeping as up to date with the latest external entity as desired.

You use the XML Registry to create entity resolution entries to identify where the external entry is located (locally or at a URL) and what the caching options are for entities on the Web. You identify the external entity entry using a system or public identifier. Then, in your XML document, when you reference this external entity, WebLogic Server fetches the local copy or the cached copy (whichever you have configured) when parsing the document.

For detailed information on creating external entity registries with the XML Registry, refer to External Entity Configuration Tasks.

Using Parsers Other Than the Default

If you use JAXP to parse your XML documents, the WebLogic Server XML Registry (which is configured through the WebLogic Server Administration Console) offers the following options:

  • Accept the default parser as the server-wide parser.

  • Configure another parser of your choice (such as a different version of the Apache Xerces parser) as the server-wide parser.

  • Configure a parser for a particular XML document type, based on its system or public identifier, or its root element.

For instructions on how to use the XML Registry to configure parsing options, see XML Parser and Transformer Configuration Tasks.

Generating New XML Documents

This section describes how to generate XML documents from a DOM document tree and by using JSP.

Note:

For detailed instructions on using the Streaming API for XML (StAX) to generate XML documents, see Using the Streaming API for XML (StAX).

Generating XML from a DOM Document Tree

You can use the javax.xml.transform.Transformer class to serialize a DOM object into an XML stream, as shown in the following example segment:

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory;  

import org.w3c.dom.Document;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;       
import javax.xml.transform.stream.StreamResult; 

import java.io.*;

...

TransformerFactory trans_factory = TransformerFactory.newInstance();
Transformer xml_out = trans_factory.newTransformer();
Properties props = new Properties();
props.put("method", "xml");
xml_out.setOutputProperties(props);
xml_out.transform(new DOMSource(doc), new StreamResult(System.out));

In the example, the Transformer.transform() method does the work of converting a DOM object into an XML stream. The transform() method takes as input a javax.xml.transform.dom.DOMSource object, created from the DOM tree stored in the doc variable, and converts it into a javax.xml.transform.stream.StreamResult object and writes the resulting XML document to the standard output.

Generating XML Documents in a JSP

You typically use JSPs to generate HTML, but you can also use a JSP to generate an XML document.

Using JSPs to generate XML requires that you set the content type of the JSP page as follows:

<%@ page contentType="text/xml"%>
    ... XML document

The following code shows an example of how to use JSP to generate an XML document:

<?xml version="1.0">

<%@ page contentType="text/xml" import="java.text.DateFormat,java.util.Date" %>

<message>
  <text>
    Hello World.
  </text>
  <timestamp>
<%
out.print(DateFormat.getDateInstance().format(new Date()));
%>
  </timestamp>
</message>

Transforming XML Documents

Transformation refers to converting an XML document (the source of the transformation) into another format, typically a different XML document, HTML, Wireless Markup Language (WML) (the result of the transformation.) This section describes how to transform XML documents using JAXP and from within a JSP using JSP tags.

Using JAXP to Transform XML Data

Version 1.2 of JAXP provides pluggable transformation, which means that you can use any JAXP-compliant transformer engine.

JAXP provides the following interfaces to transform XML data into a variety of formats:

  • javax.xml.transform

    Contains the generic APIs for transforming documents. This package does not have any dependencies on SAX or DOM and makes the fewest possible assumptions about the format of the source and result.

  • javax.xml.transform.stream

    Implements stream- and URI-specific transformation APIs. In particular, it defines the StreamSource and StreamResult classes that enable you to specify InputStreams and URLs as the source of a transformation and OutputStreams and URLs as the results, respectively.

  • javax.xml.transform.dom

    Implements DOM-specific transformation APIs. In particular, it defines the DOMSource and DOMResult classes that enable you to specify a DOM tree as either the source or result, or both, of a transformation.

  • javax.xml.transform.sax

    Implements SAX-specific transformation APIs. In particular, it defines the SAXSource and SAXResult classes that enable you to specify org.xml.sax.ContentHandler events as either the source or result, or both, of a transformation.

Transformation encompasses many possible combinations of inputs and outputs.

Example of Transforming an XML Document Using JAXP

The following example snippet shows how to use JAXP to transform myXMLdoc.xml into a different XML document using the mystylesheet.xsl stylesheet:

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;

Transformer trans;
TransformerFactory factory = TransformerFactory.newInstance();
String stylesheet = "file://stylesheets/mystylesheet.xsl";
String xml_doc = "file://xml_docs/myXMLdoc.xml";

trans = factory.newTransformer(new StreamSource(stylesheet));
trans.transform(new StreamSource(xml_doc),
                 new StreamResult(System.out));

For an example of how to transform a DOM document into an XML stream, see Using JAXP to Transform XML Data.

Using the JSP Tag to Transform XML Data

WebLogic Server provides a small JSP tag library for convenient access to an XSLT transformer from within a JSP. You can use this tag to transform XML documents into HTML, WML, and so on.

Note:

This feature is deprecated in WebLogic Server 12.1.2.0 and may be removed in a future release.

The JSP tag library consists of one main tag, x:xslt, and two subtags you can use within the x:xslt tag: x:stylesheet and x:xml.

Note:

The JSP tag library is provided for convenience only; the tag library is not required to access XSLT transformers from within a JSP.

XSLT JSP Tag Syntax

The XSLT JSP tag syntax is based on XML. A JSP tag consists of a start tag, an optional body, and a matching end tag. The start tag includes the element name and optional attributes.

Note:

This feature is deprecated in WebLogic Server 12.1.2.0 and may be removed in a future release.

The following syntax describes how to use the three XSLT JSP tags provided by WebLogic Server in a JSP. The attributes are optional, as are the subtags x:stylesheet and x:xml. The tables following the syntax describe the attributes of the x:xslt and x:stylesheet tags; the x:xml tag does not have any attributes.

<x:xslt  [xml="uri of XML file"]
         [media="media type to determine stylesheet"]
         [stylesheet="uri of stylesheet"]
    <x:xml>In-line XML goes here
    </x:xml>
    <x:stylesheet [media="media type to determine stylesheet"]
                  [uri="uri of stylesheet"]
    </x:stylesheet>
</x:xslt>

Table 3-1 describes the attributes of the x:xslt tag.

Table 3-1 x:xslt JSP Tag Attributes

x:xslt Tag Attribute Required Data Type Description

xml

No

String

Specifies the location of the XML file that you want to transform. The location is relative to the document root of the Web application in which the tag is used.

media

No

String

Defines the document output type, such as HTML or WML, that determines which stylesheet to use when transforming the XML document.

This attribute can be used in conjunction with the media attribute of any enclosed x:stylesheet tags within the body of the x:xslt tag. The value of the media attribute of the x:xslt tag is compared to the value of the media attribute of any enclosed x:stylesheet tags. If the values are equal, then the stylesheet specified by the uri attribute of the x:stylesheet tag is applied to the XML document.

Note: It is an error to set both the media and stylesheet attributes within the same x:xslt tag.

stylesheet

No

String

Specifies the location of the stylesheet to use to transform the XML document. The location is relative to the document root of the Web application in which the tag is used.

Note: It is an error to set both the media and stylesheet attributes within the same x:xslt tag.

Table 3-2 describes the attributes of the x:stylesheet tag.

Table 3-2 x:stylesheet JSP Tag Attributes

x:stylesheet Tag Attribute Required Data Type Description

media

No

String

Defines the document output type, such as HTML or WML, that determines which stylesheet to use when transforming the XML document.

Use this attribute in conjunction with the media attribute of enveloping x:xslt tag. The value of the media attribute of the x:xslt tag is compared to the value of the media attribute of the enclosed x:stylesheet tags. If the values are equal, then the stylesheet specified by the uri attribute of the x:stylesheet tag is applied to the XML document.

uri

No

String

Specifies the location of the stylesheet to use when the value of the media attribute matches the value of the media attribute of the enveloping x:xslt tag. The location is relative to the document root of the Web application in which the tag is used.

XSLT JSP Tag Usage

The x:xslt tag can be used with or without a body, and its attributes are optional. This section describes the rules that dictate how the tag behaves depending on whether you specify a body or one or more attributes.

Note:

This feature is deprecated in WebLogic Server 12.1.2.0 and may be removed in a future release.

If the x:xslt JSP tag is an empty tag (no body), the following statements apply:

  • If no attributes are set, the XML document is processed using the servlet path and the default media stylesheet. You specify the default media stylesheet in your XML file with the <?xml-stylesheet> processing instruction; the default stylesheet is the one that does not have a media attribute.

    This type of processing allows you to register the JSP page that contains the tag extension as a file servlet that performs XSLT processing.

  • If only the media attribute is set, the XML document is processed using the servlet path and the specified media type. The value of the media type attribute of the x:xslt tag is compared to the value of the media attribute of any <?xml-stylesheet> processing instructions in your XML document; if any match then the corresponding stylesheet is applied. If none match then the default media stylesheet is used. The media type attribute is used to define the document output type (for example, XML, HTML, postscript, or WML). This feature enables you to organize stylesheets by document output type.

  • If only the xml attribute is set, the specified XML document is processed using the default media stylesheet.

  • If the media and xml attributes are set, the specified XML document is processed using the specified media type.

  • If the stylesheet attribute is defined, the XML document is processed using the specified stylesheet.

    Note:

    It is an error to set both the media and stylesheet attributes within the same x:xslt tag.

An XSLT JSP tag that has a body may contain <x:xml> tags and/or <x:stylesheet> tags. The following statements apply:

  • The <x:xml> tag allows you specify an XML document for inline processing. This tag has no attributes.

  • The <x:stylesheet> tag, when used without any attributes, allows you specify the default stylesheet inline.

  • Use the uri attribute of the <x:stylesheet> tag to specify the location of the default stylesheet.

  • If you want to specify different stylesheets for different media types, you can use multiple <x:stylesheet> tags with different values for the media attribute. You can specify a stylesheet for each media type in the body of the tag, or specify the location of the stylesheet with the uri attribute.

Example of Using the XSLT JSP Tag in a JSP

The following snippet of code from a JSP shows how to use the XSLT JSP tag to transform XML into HTML or WML, depending on the type of client that is requesting the JSP. If the client is a browser, the JSP returns HTML; if the client is a wireless device, the JSP returns WML.

Note:

This feature is deprecated in WebLogic Server 12.1.2.0 and may be removed in a future release.

First the JSP uses the getHeader() method of the HttpServletRequest object to determine the type of client that is requesting the JSP and sets the myMedia variable to wml or html appropriately. If the JSP set the myMedia variable to html, then it applies the html.xsl stylesheet to the XML document contained in the content variable. Similarly, if the JSP set the myMedia variable to wml, then it applies the wml.xsl stylesheet.

<%
   String clientType = request.getHeader("User-Agent");
   // default to WML client
   String myMedia = "wml";

   // if client is an HTML browser

   if (clientType.indexOf("Mozilla") != -1) {
      myMedia = "http"
   }
%>

<x:xslt media="<%=myMedia%>">
  <x:xml><%=content%></x:xml>
  <x:stylesheet media="html" uri="html.xsl"/>
  <x:stylesheet media="wml"  uri="wml.xsl"/>
</x:xslt>

Using Transformers Other Than the Default Transformer

The WebLogic Server XML Registry (which you configure using the WebLogic Server Administration Console) offers the following options:

  • Accept the default transformer as the server-wide transformer.

  • Configure a transformer other than the default transformer as the server-wide transformer. The transformer must be JAXP-compliant.

For instructions on how to use the XML Registry to configure transforming options, see Configuring a Parser or Transformer Other Than the Default.