BEA Logo BEA WebLogic Server Release 6.1

  BEA Home  |  Events  |  Solutions  |  Partners  |  Products  |  Services  |  Download  |  Developer Center  |  WebSUPPORT

 

  |  

  WebLogic Server Doc Home   |     Programming XML   |   Previous Topic   |   Next Topic   |   Contents   |   Index   |   View as PDF

XML Programming Techniques

 

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

 


Sending and Receiving XML To and From Servlets and JSPs

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 and receive XML data from a Java client to a WebLogic Server-hosted Servlet or JSP and back, use the java.net.URLConnection 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 send and receive XML data to and from a JSP:

import java.net.*;
import java.io.*;
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 = fr.read(buffer)) != -1)
          {
            URLConnection urlc = url.openConnection();
            urlc.setRequestProperty("Content-Type","text/xml");
            urlc.setDoOutput(true);
            urlc.setDoInput(true);
            PrintWriter pw = new PrintWriter(urlc.getOutputStream());
            // send xml to jsp
            pw.write(buffer, 0, bytes_read);
            pw.close();
            BufferedReader in = new BufferedReader(new 
InputStreamReader(urlc.getInputStream()));
            String inputLine;
            while ((inputLine = in.readLine()) != null)
                      System.out.println(inputLine);
            in.close();
            }              
          }
          catch (Exception e) {
          e.printStackTrace();
          }
       }
    }
}

The example first shows how to open a URL connection to the JSP 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 shows 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 specifically handle XML documents in an 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 an 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 one. 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.

 


XML Document Header Information

Sometimes you might want to only get information about an XML document, such as the root element, system ID, or public ID, instead of getting all the actual data within the document. In this case, fully parsing the document is unnecessary, and indeed 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 about the XML document by using the weblogic.xml.sax.XMLInputSource class, which is Weblogic Server's extension to the org.xml.sax.InputSource class. The following example segment shows how to use this 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.

 

back to top previous page next page