Programming WebLogic XML
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
The following sections describe how to use the Java programming language and WebLogic Server to develop XML applications. It is assumed that you know how to use Java Servlets and Java Server Pages (JSPs) to write Java applications. For information about how to write servlet and JSP applications, see Developing Web Applications, Servlets, and JSPs for WebLogic Server.
Programmers using the WebLogic Server XML subsystem typically perform some or all of the following programming tasks when developing XML applications:
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.
For more information on this task, refer to Parsing XML Documents.
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.
For more information on this task, refer to Generating New XML Documents.
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.
For more information on this task, refer to Using JAXP to Transform XML Data.
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 Administration Console XML Registry to configure the following:
For detailed information on how to use the Administration Console for these tasks, refer to Administering WebLogic Server XML.
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.
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.
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.
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.
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.
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:
SAXParserFactory.setValidating()
method to true, as shown in the following example:SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
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.
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 J2EE 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">
Use the following WebLogic Server features to improve the performance of external entity resolution in your XML applications:
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.
If you use JAXP to parse your XML documents, the WebLogic Server XML Registry (which is configured through the Administration Console) offers the following options:
For instructions on how to use the XML Registry to configure parsing options, see XML Parser and Transformer Configuration Tasks.
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).
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.
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>
For more information about using JSP to generate XML, see http://java.sun.com/products/jsp/html/JSPXML.html.
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.
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:
This package 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
: This package 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
: This package 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
: This package 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.
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.
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.
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.
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.
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>
The following table describes the attributes of the x:xslt
tag.
The following table describes the attributes of the x:stylesheet
tag.
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.
If the x:xslt
JSP tag is an empty tag (no body), the following statements apply:
<?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.
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.xml
attribute is set, the specified XML document is processed using the default media stylesheet.media
and xml
attributes are set, the specified XML document is processed using the specified media type.stylesheet
attribute is defined, the XML document is processed using the specified stylesheet.Caution: 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:
<x:xml>
tag allows you specify an XML document for inline processing. This tag has no attributes.<x:stylesheet>
tag, when used without any attributes, allows you specify the default stylesheet inline. uri
attribute of the <x:stylesheet>
tag to specify the location of the default stylesheet.<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.To use an XSLT JSP tag to transform XML documents, perform the following steps:
xmlx.zip
file in the WL_HOME
\server\ext
directory; extract the xmlx-tags.jar
file; and put it in the /lib
directory of your Web application, where BEA Home
is the top-level directory in which you installed the WebLogic Server distribution.<taglib>
<taglib-uri>xmlx.tld</taglib-uri>
<taglib-location>/WEB-INF/lib/xmlx-tags.jar</taglib-location>
</taglib>
<%@ taglib uri="xmlx.tld" prefix="x"%>
<%@ taglib uri="xmlx.tld" prefix="x"%><x:xslt/>
<servlet>
<servlet-name>myxsltinterceptor</servlet-name>
<jsp-file>xslt.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>myxsltinterceptor</servlet-name>
<url-pattern>/xslt/*</url-pattern>
</servlet-mapping>
xslt
prefix to the pathname for the XML document (for example, change docs/fred.xml
to xslt/docs/fred.xml
) and then access the document. Because of the <url-pattern>
entry in the web.xml
file, WebLogic Server automatically runs the XSLT transformer on the XML document and sets the default stylesheet in the document.Note: The other forms of the XSLT JSP tag are used when stylesheets are not specified in the XML document or your XML stylesheet can be generated inline.
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.
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>
The WebLogic Server XML Registry (which you configure using the Administration Console) offers the following options:
For instructions on how to use the XML Registry to configure transforming options, see Configuring a Parser or Transformer Other Than the Default.
![]() ![]() |
![]() |
![]() |