Skip navigation.

Programming WebLogic XML

  Previous Next vertical dots separating previous/next from contents/index/pdf Contents Index View as PDF   Get Adobe Reader

XML Programming Techniques

The following sections provide information about specific XML programming techniques for developing a J2EE application that processes XML data:


Transmitting XML Data Between A Java Client and WebLogic Server

In a typical J2EE application, a client application sends XML data to a servlet or a JSP that processes the XML data. The servlet or JSP then either sends the data on to another J2EE component, such as a JMS destination or an EJB, or sends the processed XML data back to the client in the form of another XML document.

To send XML data from a Java client to a WebLogic Server-hosted servlet or JSP which then returns the data to the client, use the class. This class represents the communication link between an application and an URL, which in this case is the URL that invokes the servlet or JSP. Instances of the URLConnection class send the XML document using the HTTP POST method.

The following Java client program from the WebLogic XML examples shows how to transmit XML data between the program and a JSP:

import java.util.*;
public class Client {
  public static void main(String[] args) throws Exception {
if (args.length < 2) {
System.out.println("Usage: java examples.xml.Client URL Filename");
else {
try {
URL url = new URL(args[0]);
String document = args[1];
FileReader fr = new FileReader(document);
char[] buffer = new char[1024*10];
int bytes_read = 0;
if ((bytes_read = != -1)
URLConnection urlc = url.openConnection();
PrintWriter pw = new PrintWriter(urlc.getOutputStream());
            // send xml to jsp
pw.write(buffer, 0, bytes_read);
            BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
catch (Exception e) {

The example shows how to open a URL connection to the JSP by using a URL from the argument list; obtain the output stream from the connection; and print the XML document provided in the argument list to the output stream, thus sending the XML data to the JSP. The example then shows how to use the getInputStream() method of the URLConnection class to read the XML data that the JSAP returns to the client application.

The following code segments from a sample JSP show how the JSP receives XML data from the client application, parses the XML document, and sends XML data back:

BufferedReader br = new BufferedReader(request.getReader());
DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
DocumentBuilder db = fact.newDocumentBuilder();
Document doc = db.parse(new InputSource(br));
PrintWriter responseWriter = response.getWriter();
responseWriter.println("<?xml version='1.0'?>");

For detailed information on programming WebLogic servlets and JSPs, see Programming WebLogic HTTP Servlets and Programming WebLogic JSP


Handling XML Documents in a JMS Application

WebLogic Server provides the following extensions to some Java Message Service (JMS) classes to handle XML documents in a JMS application:

If you use the XMLMessage class to send and receive XML documents in a JMS application, rather than the more generic TextMessage class, you can use XML-specific message selectors to filter unwanted messages. In particular, you can use the method JMS_BEA_SELECT to specify an XPath query to search for an XML fragment in the XML document. Based on the results of the query, a message consumer might decide not to receive the message, thus possibly reducing network traffic and improving performance of the JMS application.

To use the XMLMessage class to contain XML messages in a JMS application, you must create either a WLQueueSession or WLTopicSession object, depending on whether you want to use JMS queues or topics, rather than the generic QueueSession or TopicSession objects, after you have created a JMS Connection. Then use the createXMLMessage() method of the WLSession interface to create an XMLMessage object.

For detailed information on using XMLMessage objects in your JMS application, see Programming WebLogic JMS.


Accessing External Entities That Do Not Have an HTTP Interface

WebLogic Server can retrieve and cache external entities that reside in external repositories, as long as they have an HTTP interface, such as a URL, that returns the entity. See External Entity Configuration Tasks for detailed information on using the XML Registry to configure external entities.

If you want to access an external entity that is stored in a repository that does not have an HTTP interface, you must create an interface. For example, assume you store the DTDs for your XML documents in a database table, with columns for the system id, public id, and text of the DTD. To access the DTD as an external entity from a WebLogic XML application, you could create a servlet that uses JDBC to access the DTDs in the database.

Because you invoke servlets with URLs, you now have an HTTP interface to the external entity. When you create the entity registry entry in the XML Registry, you specify the URL that invokes the servlet as the location of the external entity. When WebLogic Server is parsing an XML document that contains a reference to this external entity, it invokes the servlet, passing it the public and system id, which the servlet can internally use to query the database.


Retrieving XML Document Header Information

Suppose you want only XML document header information, such as the root element, system ID, or public ID, instead of all the actual data within the document. Fully parsing the document is unnecessary and might decrease the performance of your application if the XML document is very large.

Instead of parsing the XML document, you can get header information by using the weblogic.xml.sax.XMLInputSource class, which is Weblogic Server's extension to the org.xml.sax.InputSource class.

Warning: The weblogic.xml.sax.XMLInputSource class is deprecated as of Version 8.1 of WebLogic Server. Instead, you should use the WebLogic XML Streaming API to get the root element, public and system ids, and namespace URI of an XML document. For more information, see Using the WebLogic XML Streaming API.

The following example segment shows how to use the XMLInputSource class:

import weblogic.xml.sax.XMLInputSource;
    String inputXML = "file://xml_docs/myXMLdoc.xml";
XMLInputSource xis = new XMLInputSource(inputXML);
String docType = xis.getRootTag();
String publicID = xis.getPublicId();
String systemID = xis.getSystemId();
String namespaceURI = xis.getNamespaceURI();

See the WebLogic Server API Reference for more information on the weblogic.xml.sax.XMLInputSource class.


Skip navigation bar  Back to Top Previous Next