Home php教程 php手册 在PHP中开发XML应用程序之基础篇

在PHP中开发XML应用程序之基础篇

Jun 13, 2016 am 10:33 AM
php w3c xml one Scalable exist Base app develop yes mark of Introduction language

一、 XML简介

  XML(可扩展的标注语言)是一种W3C标准,主要用于Web应用程序和服务器之间实现容易的交互、数据的存储与使用。

  使用XML标准编码的数据具有能容易被人和计算机解释的意义和结构。XML数据是平台和应用程序独立的。不用多说,这本身就使XML成为适合于互联网的一个理想的数据交换格式(事实上,它正是因这一用途而被开发的)。最近,宽带连接的增长及消费者对于越过任何媒体进行数据共享的应用软件的需求意味着,XML Web服务和应用软件正变得越来越丰富。

  XML的发明正是为了解决描述网上丰富的数据的组织问题;而目前为止,这一问题仅能够通过HTML的巧妙使用得到部分地解决。

  下面是一XML文档的实例:

<?xml version="1.0"?>
<party>
 <location>My House</location>
 <time>7pm</time>
 <guest>
  <name>John Bloggs</name>
  <item>Crate of Fosters</item>
 </guest>
 <guest>
  <name>Sara Bloggs</name>
  <item>Umbrella</item>
 </guest>
 <guest>
  <name>David Fig</name>
  <item>Bombay Mix</item>
 </guest>
</party>

  如果你以前没见过XML,那么你可以认为它看起来象HTML。HTML是一种SGML应用程序,而XML是它的一个子集。然而,其相似性还包括它们具有相似的标注分隔符。

  仅需看一下上面的XML片断,我们就能看到,该数据是描述一个具有一些客人的聚会;其中,每一个客人相应于一项。用于描述数据的标签名完全由作者来选择。所有XML标准要求:数据必须是一致的并且用于描述数据的标签为良构的。我们可以进一步用一种文档类型声明(DTD)或一个XML模式来强制数据的完整性。然而为简化起见,我们在本文中将仅使用普通的XML。

  二、 XML应用程序

  刚才,我们已经看到了如何使用XML来描述任何种类的数据。事实上,XML已经在今天的许多Web应用程序中得到广泛使用,下面是一些著名的应用描述:

  · XHTML-这是使用最广泛的XML应用程序之一。它类似基于HTML的SGML-用于描述数据在网页上的显示方式。XHTML使用一DTD来确保所有的文档遵循标准。XHTML的出现使Web程序员的开发稍微容易了一些;然而,一种完全兼容于CSS和XHTML标准的web浏览器尚未出现。

  · XML-RPC-远程过程调用(RPC),应用于分布式应用程序中以调用远程计算机上的过程。XML-RPC使用XML对关于过程调用的信息进行编码,并且使用HTTP把它发送到接收计算机。然后,过程的返回值被再次用XML编码并用HTTP连接发送回调用者计算机。

  · RSS-真正简单的聚合/丰富的站点摘要,它是一种用来聚合web站点内容(例如新闻、文章、共享价格和链接等)的方法,它用一个特殊的应用程序(一个聚合器)定期更新用户PC上的RSS回馈。该RSS数据是使用XML进行编码和传输的。

  · AJAX-异步的JavaScript和XML,允许web开发者创建具有丰富特征的事件驱动的运行在web浏览器上的web应用程序。其中,JavaScript用于把XML编码的数据发送到服务器端脚本(或从服务器端接收XML编码的数据),并允许局部的实时的页面更新而不需要更新所有页面内容。

  上面仅仅是XML的可能的应用的一部分。在以后文章中,我们将分析如何在PHP中使用这些应用软件。

  三、 在PHP中使用XML

  自从PHP 5.0以来,PHP能与XML交互的可用选项显著地增加。而PHP版本4所能提供的是不稳定的而且是非w3c兼容的DOM XML扩展。
下面,我将集中讨论PHP 5所提供给我们的三个允许我们与XML交互的方法:DOM,简单XML和XPath。在可能之处,我将建议最适合于每种方法的条件和数据。所有的示例代码将使用XML数据源来描述一个库及其中包含的书。

<xml version="1.0"?>
<library>
 <categories>
  <category cid="1">Web Development</category>
  <category cid="2">Database Programming</category>
  <category cid="3">PHP</category>
  <category cid="4">Java</category>
 </categories>
 <books>
 <book>
  <title>Apache 2</title>
  <author>Peter Wainwright</author>
  <publisher>Wrox</publisher>
  <category>1</category>
 </book>
 <book>
  <title>Advanced PHP Programming</title>
  <author>George Schlossnagle</author>
  <publisher>Developer Library</publisher>
  <category>1</category>
  <category>3</category>
 </book>
 <book>
  <title>Visual FoxPro 6 - Programmers Guide</title>
  <author>Eric Stroo</author>
  <publisher>Microsoft Press</publisher>
  <category>2</category>
 </book>
 <book>
  <title>Mastering Java 2</title>
  <author>John Zukowski</author>
  <publisher>Sybex</publisher>
  <category>4</category>
 </book>
</books>
</library>
 四、 DOM

  DOM PHP扩展名允许使用W3C DOM API在XML文档上进行操作。在PHP 5出现之前,这是PHP能存取XML文档的唯一方法。如果你在JavaScript中使用了DOM,那么会认识到这些对象模型几乎是一样的。

  由于DOM方法在遍历和操作XML文档时比较罗嗦,所以任何DOM兼容的代码都有明显的优点-与任何其它实现相同的W3C兼容的对象模型的API兼容。

  在下面的实例代码中,我们使用DOM来显示关于每本书的信息。首先,我们遍历一下列表目录,把它们的Id和相应的名字装载到一个索引数组中。然后,我们显示每本书的一个简短描述:

  PHP:

<?php
 /*这里我们必须指定XML版本:也即是1.0 */
 $xml = new DomDocument(1.0);
 $xml->load(xml/library.xml);
 /*首先,创建一个目录列表*/
 $categories = array();
 $XMLCategories = $xml->getElementsByTagName(categories)->item(0);
 foreach($XMLCategories->getElementsByTagName(category) as $categoryNode) {
  /*注意我们是如何得到属性的*/
  $cid = $categoryNode->getAttribute(cid);
  $categories[$cid] = $categoryNode->firstChild->nodeValue;
 }
?>
<html>
<head>
<title>XML Library</title>
</head>
<body>
<?
 php foreach($xml->getElementsBytagName(book) as $book):
 /*查找标题*/
 $title = $book->getElementsByTagName(title)->item(0)->firstChild->nodeValue;
 /*查找作者-为了简化起见,我们假设仅仅有一个作者*/
 $author = $book->getElementsByTagName(author)->item(0)->firstChild->nodeValue;
 /* 列表目录*/
 $bookCategories = $book->getElementsByTagName(category);
 $catList = ;
 foreach($bookCategories as $category) {
  $catList .= $categories[$category->firstChild->nodeValue] . , ;
 }
 $catList = substr($catList, 0, -2); ?>
<div>
Statement of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 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: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

See all articles