Use PHP and AJAX for XML programming _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags wrappers xquery
Use PHP and AJAX for XML programming. In SOA architecture, programmers often use XML to exchange structured and semi-structured data between applications. XML and its related technologies-Document Object Model (DocumentObjectModel, D) in the 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.

  


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 (Document Object Model, D...

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.