Home > Backend Development > PHP Tutorial > Basics of developing XML applications in PHP_PHP Tutorial

Basics of developing XML applications in PHP_PHP Tutorial

WBOY
Release: 2016-07-13 17:34:21
Original
780 people have browsed it

1. Introduction to XML

XML (Extensible Markup Language) is a W3C standard mainly used for easy interaction, data storage and use between Web applications and servers.

Data encoded using the XML standard has meaning and structure that can be easily interpreted by humans and computers. XML data is platform and application independent. Needless to say, this in itself makes XML an ideal data exchange format for the Internet (in fact, it was developed for this very purpose). Recently, the growth of broadband connections and consumer demand for applications that share data across any medium mean that XML Web services and applications are becoming increasingly rich.

XML was invented to solve the organizational problem of describing the rich data on the Web; so far, this problem can only be partially solved through the clever use of HTML.

The following is an example of an XML document:

<?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>
<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>

If you haven't seen XML before, you can assume that it looks like HTML. HTML is a SGML application, and XML is a subset of it. However, the similarity also includes that they have similar label separators.

Just by looking at the XML snippet above, we can see that the data describes a party with a number of guests; one entry for each guest. The label names used to describe the data are entirely chosen by the author. All XML standards require that the data must be consistent and the tags used to describe the data must be well-formed. We can further enforce data integrity using a document type declaration (DTD) or an XML schema. However, for simplicity, we will only use plain XML in this article.

2. XML application

Just now, we have seen how to use XML to describe any kind of data. In fact, XML has been widely used in many web applications today. Here are some famous application descriptions:

· XHTML - This is one of the most widely used XML applications. It is similar to SGML based on HTML - used to describe how data is displayed on a web page. XHTML uses a DTD to ensure that all documents adhere to the standard. The emergence of XHTML has made development slightly easier for Web programmers; however, a web browser that is fully compatible with the CSS and XHTML standards has not yet emerged.

XML-RPC - Remote Procedure Call (RPC), used in distributed applications to call procedures on remote computers. XML-RPC uses XML to encode information about the procedure call and sends it to the receiving computer using HTTP. The return value of the procedure is then encoded again in XML and sent back to the caller's computer using an HTTP connection.

· RSS - Really Simple Syndication/Rich Site Summary, which is a method for aggregating web site content (such as news, articles, share prices, links, etc.) using a special application (an aggregator) Regularly update RSS feedback on users' PCs. This RSS data is encoded and transmitted using XML.

· AJAX - Asynchronous JavaScript and XML that allows web developers to create feature-rich, event-driven web applications that run in a web browser. Among them, JavaScript is used to send XML-encoded data to server-side scripts (or receive XML-encoded data from the server-side), and allows partial real-time page updates without updating all page content.

The above are just some of the possible applications of XML. In future articles, we will analyze how to use these applications in PHP.

3. Using XML in PHP

Since PHP 5.0, the options available for PHP to interact with XML have increased significantly. What PHP version 4 can provide is an unstable and non-w3c compatible DOM XML extension.
Below, I will focus on the three methods provided by PHP 5 that allow us to interact with XML: DOM, simple XML and XPath. Where possible, I will suggest conditions and data that are most appropriate for each approach. All sample code will use an XML data source to describe a library and the books it contains.

<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>
4. DOM

The DOM PHP extension allows operations on XML documents using the W3C DOM API. Before PHP 5, this was the only way PHP could access XML documents. If you've used the DOM in JavaScript, you'll realize that these object models are almost identical.

Because DOM methods for traversing and manipulating XML documents are somewhat verbose, any DOM-compliant code has the distinct advantage of being compatible with any other API that implements the same W3C-compliant object model.

In the example code below, we use DOM to display information about each book. First, we traverse the list directories and load their IDs and corresponding names into an index array. We then display a short description of each book:

PHP:

<?php
/*Here we must specify the XML version: which is 1.0 */
​$xml = new DomDocument(1.0);
$xml->load(xml/library.xml);
/*First, create a directory list*/
​$categories = array();
​$XMLCategories = $xml->getElementsByTagName(categories)->item(0);
​foreach($XMLCategories->getElementsByTagName(category) as $categoryNode) {
/*Note how we get the attributes*/
​$cid = $categoryNode->getAttribute(cid);
​$categories[$cid] = $categoryNode->firstChild->nodeValue;
}
?>
<html>
<head>
<title>XML Library</title>
</head>
<body>
<?
php foreach($xml->getElementsBytagName(book) as $book):
/*Find title*/
$title = $book->getElementsByTagName(title)->item(0)->firstChild->nodeValue;
/*Find the author-for simplicity, we assume there is only one author*/
$author = $book->getElementsByTagName(author)->item(0)->firstChild->nodeValue;
/* List directory*/
​$bookCategories = $book->getElementsByTagName(category);
​$catList = ;
​foreach($bookCategories as $category) {
​$catList .= $categories[$category->firstChild->nodeValue] . , ;
}
​$catList = substr($catList, 0, -2); ?>
<div>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508481.htmlTechArticle1. Introduction to XML XML (Extensible Markup Language) is a W3C standard mainly used for Web applications Implement easy interaction, data storage and use with the server. Compiled using XML standards...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template