In the SOA architecture, programmers often use XML to exchange structured and semi-structured data between applications. XML and its related technologies-Document Object model (Doc object model,dom), XPath, HTTP, XQuery, and Extensible Stylesheet Language Transformations (extensible Stylesheet Language TRANSFORMATIONS,XSLT)-Provides a powerful environment for rapid application development. Applications built on these technologies will take up less memory space, require lower maintenance costs, and have higher quality and flexibility.
The XML aspects of DB2 and other relational databases are already fairly mature, so they are ideal for storing and managing XML data in addition to storing and managing relational data. DB2 9 XML Support (called PureXML) provides the ability to store XML in the form of pure XML (in other words, annotated, tree-layered storage). In DB2 9, XML data can be indexed in XML schemas, can be combined from relational data, can be decomposed into relational data, can be queried and transformed, can be published independently, or combined with relational data by mixing sql/xml and XQuery.
Web browsers are also providing more functionality for client script to handle XML efficiently. By using asynchronous JavaScript and XML (asynchronous JavaScript and Xml,ajax), Web pages can now make remote procedure calls directly to the application server and can use the DOM API on any returned XML data.
This article will show you how to write a simple XML-based application using the features provided by DB2 XML, Ajax, and PHP hypertext Preprocessor (PHP). With the help of the sample scenario, you will learn how to invoke PHP applications in JavaScript, how to modify XML data using the DOM and SimpleXML APIs, how to transfer XML from the client to the application and the database, and how to create a PHP Web service to use Sql/xml and XQuery Publish reports on XML data.
XML Benefits
Most applications are used to create, store, manipulate, and render business data. Object wrapping refers to tying business data together to make it easier for business logic to handle them. Many of the functions of these wrapper objects are based on relational and formatting rules that provide the structure of business data and enable business logic to manipulate, publish, and serialize encapsulated data.
Figure 1. Object Wrapper-based applications
Figure 1 illustrates a sample life insurance application that uses the object wrapper. Each box represents an object, and each object has at least:
• One constructor
· Getter and Setter methods
• Verification Code
• Serialization of the internal object hierarchy
These objects are not related to the actual business logic. Object wrapping is intended to make business logic easier to manage business data. The code required to wrap the data is much more than the business logic. More code will lead to more bugs, greater retention, more maintenance, and higher costs.
If a data variable in an object can be formatted as an XML structure, and the primary purpose of the object is to expose the data to business logic and have the business logic manipulate them, you can use the DOM instead of the object.
Figure 2. XML-based applications
Figure 2 shows a sample insurance application that uses XML and DOM wrappers. All the data wrapper objects in Figure 1 are replaced with a DOM object. Business data is modeled with XML, and the DOM provides the necessary APIs to:
• Create a new XML object.
• Update the value of the XML object.
• Navigating XML objects.
• Use XPath to search in the object hierarchy.
• Serialize and crossdress the hierarchy of XML objects (in other words, built-in persistence).
By using XML, you can avoid using most wrapper objects for managing business data. Applications will become more concise and focus more on business logic than on data management.
XML and schemas
Introducing XML into the schema can provide a standardized way to represent business data. XML can provide the structure of data; The XML schema imposes structure and formatting rules; the DOM API and languages such as XQuery, XPath, and XSLT enable business logic to manipulate, publish, and serialize data efficiently. Because the XML representation of business data is consistent in the client, middle tier, and database, the code that manipulates these objects is similar.
I'll show you how to build an XML-based application in a three-tier environment, a three-tier environment that consists of the following components:
· WEB Client: Asynchronous JavaScript and XML (Ajax), DOM
• Application Server: PHP and SimpleXML
• Databases: DB2 9 and Sql/xml,xquery
Scenarios based on the ACORD life data model
Let's consider a simple life insurance scenario in which you first create an XML document that represents the new policy, then query and manipulate the document, and move the document from one layer to another. This document is based on the XML for the life Insurance specification of the Cooperative Operations Research and Development Association (Association for Cooperative Operations Study & Development,acord) It defines the data that health insurance and annuity insurance need to exchange.
In order to apply for a new insurance, the customer needs to provide some basic information. Some of the information is filled out in a PHP application, and part of it is written in the client browser. The policy is then stored in a DB2 XML column. In DB2 9, XML-type columns internally store the XML data as a parsed tree, stored in a different place than the relational data. This approach is unique to DB2 9, and the earlier version of DB2 uses a relational storage infrastructure to store XML.
The following is the process of the policy XML document between the client and the application:
• In the Web client, the Customer updates the page and clicks Submit.
· The WEB client sends a XMLHTTP request to PHP to obtain a 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 the returned events, retrieve the XML DOM, and populate the document with the information entered in the Web page.
· The WEB client uses XMLHTTP to send the updated XML to the PHP application.
Figure 3. Create a new policy request for the WEB site.
Figure 3 shows the Web page used to create a new policy request. When the user clicks the Submit button, the JavaScript function Submitpolicy () is called (see Listing 1). The 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 the events returned from the HTTP request.
When the first request arrives at the middle-tier PHP application Server, a new XML policy document is loaded into the SimpleXML object. Update the Transrefguid element with the GUID created in the PHP application by using the 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.
For this article, we assume that GUIDs are created by some mechanism, such as a combination of time and random numbers. It is more important to understand how the XML document that represents the policy is treated as a business object hierarchy in memory, and how to use the SimpleXML API (or Dom/xpath) to navigate and update the object.
Populate Basic Customer Information
In the Web client, the Fillpolicy () function reads the returned value. Now, the DOM object that contains the in-memory representation of the returned XML can be used to manipulate the policy document. The information that the customer enters on the Web page is used directly to update the DOM. Once the policy has been updated with customer information, use XMLHTTP to submit the modified DOM object back to the PHP application (see Listing 2). Even the HTML component values are read with the Dhtmldocument Object Model (DOM).
http://www.bkjia.com/PHPjc/446991.html www.bkjia.com true http://www.bkjia.com/PHPjc/446991.html techarticle In the SOA architecture, programmers often use XML to exchange structured and semi-structured data between applications. XML and its related technologies-Document Object Model Model,d ...