Skip navigation.

Programming WebLogic XML

  Previous Next vertical dots separating previous/next from contents/index/pdf Contents 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 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 transmit XML data between the program and 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 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 Developing Web Applications, Servlets, and JSPs for WebLogic Server .

 


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.

 

Skip navigation bar  Back to Top Previous Next