search
HomeBackend DevelopmentPHP TutorialXML programming using PHP and AJAX_PHP tutorial

XML programming using PHP and AJAX_PHP tutorial

Jul 15, 2016 pm 01:22 PM
ajaxphpxmlexchangeuseandexistappArchitectureofprogrammerprogramming

  在 SOA 架构中,程序员经常使用 XML 在应用程序之间交换结构化和半结构化的数据。XML 及其相关技术 — 文档对象模型(Document Object Model,DOM)、XPath、HTTP、XQuery 和可扩展样式表语言转换(Extensible Stylesheet Language Transformations,XSLT) — 为快速应用程序开发提供了一个强大的环境。构建在这些技术之上的应用程序将占用更小的内存空间,需要更低的维护成本,同时又拥有更高的品质和灵活性。

  DB2 和其他关系数据库的 XML 方面的特性已经相当成熟,因此除了存储和管理关系数据之外,它们还是存储和管理 XML 数据的理想选择。DB2 9 XML 支持(称为 pureXML)提供了以纯 XML 的形式(换句话说,就是带注释的、树型的分层存储)存储 XML 的能力。在 DB2 9 中,XML 数据可以用 XML 模式索引,可以从关系数据组合而成,可以分解为关系数据,可以查询和转换,可以独立发布,或者通过混合使用 SQL/XML 和 XQuery 与关系数据组合起来。

  Web 浏览器也正在为客户机脚本提供更多的功能来有效地处理 XML。通过使用异步 JavaScript 和 XML(Asynchronous JavaScript and XML,Ajax),Web 页面现在可以直接对应用服务器进行远程过程调用,并且可以在任何返回的 XML 数据上使用 DOM API。

  本文将展示如何利用 DB2 XML、Ajax 和 PHP Hypertext Preprocessor (PHP) 提供的功能来编写简单的基于 XML 的应用程序。通过示例场景的帮助,您将学习如何在 JavaScript 中调用 PHP 应用程序;如何使用 DOM 和 SimpleXML API 修改 XML 数据;如何将 XML 从客户机传送到应用程序再到数据库;以及如何创建 PHP Web 服务来使用 SQL/XML 和 XQuery 发布关于 XML 数据的报告。

  XML 优点

  大多数应用程序都用于创建、存储、操纵和呈现业务数据。对象包装是指将业务数据绑在一起,使业务逻辑更容易处理它们。这些包装器对象的很多功能都是根据关系和格式化规则来提供业务数据的结构,并使业务逻辑能够操纵、发布和串行化封装的数据。

  图 1. 基于对象包装器的应用程序

  

图 1. 基于对象包装器的应用程序

  图 1 阐释了使用对象包装器的一个示例人寿保险应用程序。每个方框表示一个对象,每个对象至少有:

  ·一个构造函数

  ·Getter 和 Setter 方法

  ·验证代码

  ·内部对象层次的串行化

  这些对象与实际的业务逻辑没有关系。对象包装是为了使业务逻辑更容易管理业务数据。包装数据所需的代码比业务逻辑要多得多。更多的代码将导致更多的 bug、更大的固定性、更多的维护和更高的成本。

如果对象中的数据变量可以格式化为 XML 结构,并且对象的主要作用是将这些数据暴露给业务逻辑并让业务逻辑操纵它们,那么可以用 DOM 代替对象。

  图 2. 基于 XML 的应用程序

  

图 2. 基于 XML 的应用程序

  图 2 展示了一个使用 XML 和 DOM 包装器的示例保险应用程序。图 1 中的所有数据包装器对象都用一个 DOM 对象代替。业务数据是用 XML 建模的,DOM 提供了必要的 API 来:

  ·创建新的 XML 对象。

  ·更新 XML 对象的值。

  ·导航 XML 对象。

  ·使用 XPath 在对象层次中搜索。

  ·串行化和反串行化 XML 对象层次(换句话说,就是内建持久性)。

  通过使用 XML,可以避免使用大多数用于管理业务数据的包装器对象。应用程序将变得更加简洁,并且更多地将重点放在业务逻辑上,而不是数据管理上。

  XML 和架构

  将 XML 引入架构中可以为表示业务数据带来一种标准化的方式。XML 可以提供数据的结构;XML 模式施加结构和格式化规则;DOM API 和 XQuery、XPath 及 XSLT 之类的语言使业务逻辑可以有效地操纵、发布和串行化数据。由于业务数据的 XML 表示在客户机、中间层和数据库中都是一致的,因此操纵这些对象的代码也是类似的。

  我将展示如何在三层环境中构建基于 XML 的应用程序,这个三层环境由以下几个部分组成:

  ·Web 客户机:Asynchronous JavaScript and XML (Ajax),DOM

  ·应用服务器:PHP 和 SimpleXML

  ·数据库: DB2 9 和 SQL/XML,XQuery

Scenario based on ACORD life data model

Let’s consider a simple life insurance scenario. In this scenario, we first create an XML document representing a new policy, and then query and manipulate this document. In addition, Move this document from one layer to another. This document is based on the Association for Cooperative Operations Research & Development (ACORD) XML specification for life insurance, which defines the data that needs to be exchanged for health insurance and annuity insurance.

In order to apply for a new insurance, customers need to provide some basic information. Some of the information is filled in a PHP application and some in the client browser. The policy is then stored in a DB2 XML column. In DB2 9, columns of type XML store XML data internally as a parsed tree, in a separate place from the relational data. This approach is specific to DB2 9; earlier DB2 versions used a relational storage infrastructure to store XML.

 The following is the flow 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 makes an 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 returns the document to the Web client.

 ·The Web client uses Ajax to capture the returned event and retrieve the XML DOM, then 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. Create a new policy request website.

 

图 3. 创建新的保单请求的 Web 站点。

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). This function makes an HTTP request to the PHP application createNewPolicy.php to obtain a blank policy. It also sets a callback function fillPolicy() that captures events returned from HTTP requests.

 

使用PHP和AJAX的XML编程

When the first request reaches 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();

  Then, this The document is sent to the client.

For this article, we assume that the GUID is created through some mechanism (such as a combination of time and random numbers). What's more important is understanding how the XML document representing the policy is viewed as an in-memory hierarchy of business objects, and how to use the 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, a DOM object containing an in-memory representation of the returned XML can be used to manipulate policy documents. Information entered by the customer on the web page is used directly to update the DOM. After the policy is updated with the customer information, the modified DOM object is submitted back to the PHP application using XMLHTTP (see Listing 2). Even the HTML component values ​​are read using the DHTMLDocument Object Model (DOM).

 

使用PHP和AJAX的XML编程


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446991.htmlTechArticleIn SOA architecture, programmers often use XML to exchange structured and semi-structured data between applications . XML and related technologies - Document Object Model (Document Object Model, D...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool