XML programming using PHP and Ajax (2)

Source: Internet
Author: User
Tags object db2 http request object model php and client

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 a 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. Part of the information is filled in a PHP application, and part of it is in the client browser. The policy is then stored in a DB2 XML column. In DB2 9, XML-type columns store XML data internally as a parsed tree, stored in different places with relational data. This approach is DB2 9-specific, and the earlier DB2 version uses the relational storage infrastructure to store the 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 event, retrieves the XML DOM, and populates 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. A Web site that creates a new policy request.
Figure 3 shows a Web page that is used to create a new policy request. When the user clicks the Submit button, the JavaScript function Submitpolicy () is invoked (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 (), that is used to catch events that are returned from HTTP requests.

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 the GUID is created by a 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 an in-memory business object hierarchy and how to use the SimpleXML API (or Dom/xpath) to navigate and update the object.

Populating basic Customer Information

In the Web client, the Fillpolicy () function reads the returned value. Now, a DOM object that contains an 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. After updating the policy with customer information, use XMLHTTP to submit the modified DOM object back to the PHP application (see Listing 2). Even HTML component values are read with the Dhtmldocument Object Model (DOM).



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.