Basics of developing XML applications in PHP

Source: Internet
Author: User
Tags php foreach
I. XML introduction XML (Extensible Markup Language) is a W3C standard used for easy interaction between Web applications and servers and data storage and use. Data encoded using XML standards has the meaning and structure that can be easily interpreted by humans and computers. XML data is SyntaxHighlighter. all () independent of the platform and application (); 1. XML introduction

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

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

XML was invented to solve the problem of describing the rich data on the internet. 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> 7 </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 have never seen XML before, you can think it looks like HTML. HTML is a SGML application, and XML is a subset of it. However, their similarity also includes their similar annotation delimiters.

You only need to take a look at the XML snippet above. we can see that this data describes a party with some guests. each guest corresponds to one. The label name used to describe the data is completely selected by the author. All XML standards require that the data must be consistent and the labels used to describe the data are constructed in a good structure. We can further use a Document Type Declaration (DTD) or an XML schema to force data integrity. However, for simplicity, we will only use common XML in this article.

   II. XML applications

Just now, we have seen how to use XML to describe data of any type. In fact, XML has been widely used in many Web applications today. Below are some well-known application descriptions:

· XHTML-this is one of the most widely used XML applications. It is similar to the HTML-based SGML-used to describe how data is displayed on a webpage. XHTML uses a DTD to ensure that all documents comply with standards. The emergence of XHTML makes Web programmer development a little easier. However, a web browser completely compatible with CSS and XHTML standards has not yet appeared.

XML-RPC-remote procedure call (RPC), used in distributed applications to call processes on remote computers. The XML-RPC uses XML to encode information about a procedure call and sends it to the receiving computer using HTTP. Then, the returned value of the process is encoded in XML again and sent back to the caller's computer through an HTTP connection.

· RSS-truly simple aggregation/Rich Site Summaries. it is a method used to aggregate website content (such as news, articles, shared prices and links, it uses a special application (a aggregator) to regularly update RSS Feedback on the user's PC. The RSS data is encoded and transmitted using XML.

· AJAX-Asynchronous JavaScript and XML allow web developers to create event-driven web applications running on web browsers with rich features. JavaScript is used to send XML-encoded data to a server-side script (or receive XML-encoded data from the server ), and allows partial real-time page updates without updating all page content.

The above is only part of the possible application of XML. In future articles, we will analyze how to use these applications in PHP.

   3. use XML in PHP

Since PHP 5.0, the number of available options for PHP to interact with XML has increased significantly. PHP version 4 provides unstable and non-w3c compatible dom xml extensions.
Next, I will focus on three methods provided by PHP 5 that allow us to interact with XML: DOM, simple XML, and XPath. Where possible, I would suggest conditions and data that best suit each method. All the sample code uses an XML data source to describe a library and its contained books.

<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 Zukoski </author>
<Publisher> Sybex </publisher>
<Category> 4 </category>
</Book>
</Books>
</Library>
  IV. DOM

The dom php extension allows you to use W3C DOM APIs to operate on XML documents. Before PHP 5 appeared, this was the only way PHP could access XML documents. If you use DOM in JavaScript, you will realize that these object models are almost the same.

Since DOM methods are copeat when traversing and operating XML documents, any DOM-compatible code has obvious advantages-it is compatible with APIs of any other object model that implements the same W3C compatibility.

In the following example code, 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. Then, we will display a brief description of each book:

PHP:

<? Php
/* Here we must specify the XML version: 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 attribute */
$ Cid = $ categoryNode-> getAttribute (cid );
$ Categories [$ cid] = $ categoryNode-> firstChild-> nodeValue;
}
?>
<Html>
<Head>
<Title> XML Library </title>
</Head>
<Body>
<?
Php foreach ($ xml-> getElementsBytagName (book) as $ book ):
/* Search for titles */
$ Title = $ book-> getElementsByTagName (title)-> item (0)-> firstChild-> nodeValue;
/* Find the author. for simplicity, assume that 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>

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.