All the messages sent over a SOAPConnection object are sent with the call() method, which sends the message and blocks until it receives the response. Thus, the return value for the call() method is the SOAPMessage object that is the response to the message that was sent.
Code Snippet 5:
SOAPConnectionFactory fact = SOAPConnectionFactory.newInstance();
SOAPConnection con = fact.createConnection();
// create a request message and give it content java.net.URL endpoint = new
URL(“http://java.sun.com/javaee/7/docs”);
SOAPMessage response = con.call(request, endpoint);
SAAJ 1.3 provides some new features. They include support for the WS-I Basic Profile 1.1, Document Object Model integration where the SAAJ APIs now extend Document Object Model API. Further, SOAPMessage properties are used for setting character set encoding and turning on the writing of an XML declaration at the start of the SOAP part of the message.
1.11 JAXB
The Java Architecture for XML Binding (JAXB) is a set of interfaces which helps client applications to communicate with code generated from a schema. JAXB allows easy access to XML documents from applications written in Java programming language.
It binds the XML schemas and Java representations very fast and makes work easy for Java developers. It enables them to incorporate XML data and process functions in Java applications. It converts XML instance documents into Java content trees and vice versa.
Introduction to Web Services
Session 01
Interface Description
Binding compiler It creates Java classes from a given schema.
Binding framework It provides runtime services such as marshalling, unmarshalling, and validation that have to be performed on the contents classes.
Binding language It describes schema binding of Java classes using which you can override the default binding rules.
Table 1.11: Components of JAXB
1.11.1 JAXB
There is a standard structure to be followed by any two parties who communicate by passing XML documents between them. This standard is defined by the standard schema facility for XML documents. This standardization enables the communicating parties to understand the contents of the documents easily.
JAXB has a good quality XML data-binding facility for the J2EE platform. Figure 1.16 shows the JAXB architecture.
Figure 1.16: JAXB Architecture
Introduction to Web Services
Session 01
A JAXB implementation consists of the following architectural components:
Schema compiler
Schema compiler binds a source schema to a set of schema derived program elements using an XML-based binding language.
Schema generator
Schema generator maps a set of existing program elements to a derived schema.
Binding runtime framework
Binding runtime framework accesses, manipulates, and validates XML content using the unmarshalling (reading) and marshalling (writing) operations.
1.11.2 JAXB Binding Process
JAXB presents an XML document in a Java format to the application to facilitate easy access to the contents. The schema for the XML document is bound into a set of Java classes that represents the schema. Figure 1.17 shows the JAXB binding process.
Figure 1.17: JAXB Binding Process The steps in the JAXB data binding process are as follows:
1. Generate classes: As the first step, the XML schema is compiled by JAXB binding compiler to generate JAXB classes.
Introduction to Web Services
Session 01
3. Unmarshal: In the third step, the XML documents are unmarshalled with the help of JAXB binding framework.
4. Generate content tree: In the fourth step, a content tree of data objects is generated by the unmarshalling process. This tree represents the structure and content of the source XML documents.
5. Validate: In the fifth step, the source XML documents are validated optionally by the unmarshalling process before generating the content tree.
6. Process content: In the sixth step, the XML data is modified by the client application.
It is represented by the Java content tree.
7. Marshal: In the final step, the processed content tree is marshalled into XML documents. These may be further validated before marshalling.
Consider an XML document named employees.xml that stores information about employees, such as id, firstName, lastName, location, and gender as shown in figure 1.18.
Figure 1.18: XML Document
An XML schema document that represents this XML document is shown in figure 1.19.
Introduction to Web Services
Session 01
Figure 1.19: XSD Document
1.11.3 Concept of Unmarshalling
The process of converting an XML document into a content tree is termed as unmarshalling.
This process includes creating content object tree that shows the content and organization of the document.
First a JAXBContext object is created. This is the entry point to the JAXB API. Then, a context path is created. This path lists the name(s) of the package(s) that contain interfaces generated by the binding compiler. Having multiple package names in the path implies that a combination of XML data elements that correspond to different schemas can be unmarshalled using JAXB. Figure 1.20 depicts unmarshalling.
Introduction to Web Services
Session 01
Figure 1.20: Unmarshalling
JAXB also allows accessing XML data without unmarshalling it. In that process, use the createCollection and createBookType methods to create a content objects tree. The program accesses the ObjectFactory class and uses the appropriate methods within it to create the required objects.
1.11.4 Unmarshalling Process
During unmarshalling, a DOM tree that represents the content of XML document is created.
The content in the tree is in the form of nodes. Code Snippet 6 demonstrates how to parse an XML document using a DOM-based parser.