In SOA architecture, programmers often use XML to exchange structured and semi-structured data between applications. XML and Related Technologies-Document Object Model (DOM), XPath, HTTP, XQuery, and Extensible style sheet Language conversion (Extensible Stylesheet Language Transformations, XSLT) -provides a powerful environment for rapid application development. Applications built on these technologies will occupy less memory space, require lower maintenance costs, and have higher quality and flexibility.
DB2 and other relational databases have mature XML features. Therefore, in addition to storing and managing relational data, they are ideal for storing and managing XML data. DB2 9 XML support (called pureXML) provides the ability to store XML in plain XML format (in other words, Hierarchical Storage with annotations and trees. In DB2 9, XML data can be indexed in XML mode and can be combined from relational data. It can be divided into relational data, can be queried and converted, and can be released independently, alternatively, you can use SQL/XML and XQuery in combination with relational data.
The Web browser is also providing more functions for client scripts to effectively process XML. By using Asynchronous JavaScript and XML (Asynchronous JavaScript and XML, Ajax), Web pages can now be called remotely on the application server, and DOM APIs can be used on any returned XML data.
This article will show you how to use the functions provided by DB2 XML, Ajax, and PHP Hypertext Preprocessor (PHP) to write simple XML-based applications. With the help of the example scenario, you will learn how to call PHP applications in JavaScript; How to Use DOM and SimpleXML APIs to modify XML data; how to transfer XML from the client to the application and then to the database; and how to create a PHP Web Service to use SQL/XML and XQuery to publish a report on XML data.
XML advantages
Most applications are used to create, store, manipulate, and present business data. Object packaging means to bind business data together to make it easier for business logic to process them. Many of these Wrappers provide a structure of business data based on relationships and formatting rules, and enable business logic to manipulate, publish, and serialize encapsulated data.
Figure 1. Application Based on Object wrapper
Figure 1 illustrates a sample life insurance application using the object wrappers. Each box indicates an object. Each object must include at least:
· A constructor
· Getter and Setter Methods
· Verification Code
· Serialization of internal object Layers
These objects have no relationship with the actual business logic. Object packaging aims to make business logic easier to manage business data. Data packaging requires much more code than business logic. More code will lead to more bugs, greater fixation, more maintenance, and higher costs.
If the data variables in an object can be formatted into an XML structure and the main function of the object is to expose the data to the business logic and allow the business logic to manipulate them, the DOM can be used to replace the object.
Figure 2. XML-based applications
Figure 2 shows an example insurance application using the XML and DOM wrappers. All data wrapper objects in Figure 1 are replaced by a DOM object. Business Data is modeled in XML, and DOM provides the necessary APIs:
· Create a New XML object.
· Update the value of the XML object.
· Navigation XML object.
· Use XPath to search at the object level.
· Serializing and deserializing XML Object layers (in other words, built-in persistence ).
By using XML, you can avoid using most of the package objects used to manage business data. Applications will become more concise and focus more on business logic rather than data management.
XML and architecture
Introducing XML into the architecture can bring about a standardized way to represent business data. XML can provide the structure of data. XML mode applies structure and formatting rules. DOM APIs and languages such as XQuery, XPath, and XSLT enable business logic to effectively manipulate, publish, and serialize data. Because the XML Representation of business data is consistent in the client, middle layer, and database, the code for manipulating these objects is similar.
I will show you how to build an XML-based application in a three-tier environment, which consists of the following parts:
· Web Client: Asynchronous JavaScript and XML (Ajax), DOM
· Application Server: PHP and SimpleXML
· Database: DB2 9, SQL/XML, and XQuery
Scenario Based on ACORD Life Data Model
Let's consider a simple life insurance scenario. In this scenario, first create an XML document that represents the new warranty order, and then query and manipulate this document, in addition, this document is moved from one layer to another. This document is based on the XML used by the Association for Cooperative Operations Research & Development, ACORD for life insurance specifications. It defines the data to be exchanged between health insurance and annuity insurance.
In order to apply for a new insurance, the customer must provide some basic information. Some information is entered in a PHP application, and some information is entered in the client browser. The policy is then stored in a DB2 XML column. In DB2 9, XML columns store XML data as a parsed tree in different places than relational data. This method is unique to DB2 9. Earlier DB2 versions use relational storage infrastructure to store XML.
The following describes the procedure of the Policy XML document between the client and the application:
· In the Web Client, update the page and click Submit.
· The Web Client sends an XMLHTTP request to PHP to obtain the new blank policy document.
· The PHP application opens a blank policy document, updates it with a globally unique identifier (GUID), and then returns the document to the Web Client.
· The Web client uses Ajax to capture returned events, retrieve the xml dom, and then fill the document with the information entered on the Web page.
· The Web client uses XMLHTTP to send the updated XML to the PHP application.
Figure 3. Create a Web site for a new policy request.
Figure 3 shows the Web page used to create a new policy request. When you click the Submit button, the JavaScript function submitPolicy () is called (see Listing 1 ). This function sends an HTTP request to the PHP application createNewPolicy. php to obtain a blank policy. It also sets a callback function fillPolicy () to capture events returned from HTTP requests.
When the first request arrives at the middle-layer PHP application server, a New XML policy document is loaded into SimpleXML objects. Update the TransRefGUID element using the GUID created in the PHP application using SimpleXML API.
Header ('content-type: text/xml ');
$ FileContents = file_get_contents ("$ basedir/acord. xml ");
$ Dom = simplexml_load_string ($ fileContents );
$ Dom-> TXLifeRequest-> TransRefGUID = $ guid;
Echo $ dom-> asXML ();
The document is then sent to the client.
In this article, we assume that the GUID is created through a mechanism (such as a combination of time and random number. More importantly, you need to understand how to regard the XML document indicating a policy as the business object hierarchy in the memory and how to use SimpleXML API (or DOM/XPath) to navigate and update this object.
Fill in basic customer information
In the Web Client, The fillPolicy () function reads the returned value. Now, DOM objects in the memory containing the returned XML can be used to manipulate the policy document. The information entered by the customer on the Web page is directly used to update the DOM. After the customer information is updated, use XMLHTTP to submit the modified DOM object back to the PHP application (see Listing 2 ). Even HTML component values are read using DHTMLDocument Object Model (DOM.