The basics of developing XML applications in PHP _php tutorial

Source: Internet
Author: User
Tags php foreach
I. Introduction to XML

XML (Extensible Markup Language) is a standard that is used primarily for easy interaction between Web applications and servers, and for the storage and use of data.

Data encoded using XML standards has the meaning and structure that can be easily interpreted by people and computers. XML data is platform-and application-independent. Needless to say, this in itself makes XML an ideal data interchange format for the Internet (in fact, it was developed for this purpose). More recently, the growth of broadband connectivity and the need for consumer applications to share data across any media means that XML Web services and applications are becoming richer.

The invention of XML is to solve the organizational problem of describing the rich data on the Internet, so far, this problem can only be partially solved by the clever use of HTML.

The following is an example of an XML document:


My House
7pm

John Bloggs
Crate of Fosters


Sara Bloggs
Umbrella


David Fig
Bombay Mix


If you haven't seen XML before, then you can think of it as HTML. HTML is an SGML application, and XML is a subset of it. However, their similarity also includes their similar label separators.

Just look at the XML fragment above, and we can see that the data describes a party with some guests, each of whom corresponds to one item. The label name used to describe the data is completely selected by the author. All XML standards require that the data be consistent and that the label used to describe the data is well-structured. We can further use a document type declaration (DTD) or an XML schema to enforce the integrity of the data. For the sake of simplicity, however, we will use only plain XML in this article.

   Second, the XML application

Just now, we've seen how to use XML to describe any kind of data. In fact, XML has been widely used in many of today's web applications, and here are some notable application descriptions:

· Xhtml-This is one of the most widely used XML applications. It resembles HTML-based sgml-to describe how data is displayed on a Web page. XHTML uses a DTD to ensure that all documents conform to the standard. The advent of XHTML makes the development of web programmers a little easier; however, a Web browser that is fully compatible with CSS and XHTML standards has not yet appeared.

· xml-rpc-Remote Procedure Call (RPC), which is applied to a distributed application to invoke a procedure on a remote computer. XML-RPC uses XML to encode information about a procedure call and sends it to the receiving computer using HTTP. The return value of the procedure is then again encoded in XML and sent back to the caller's computer using an HTTP connection.

· Rss-is a truly simple aggregation/rich Site Summary, a method used to aggregate Web site content (such as news, articles, shared prices, and links), which periodically updates the RSS feeds on the user's PC with a special application (an aggregator). The RSS data is encoded and transmitted using XML.

· ajax-asynchronous JavaScript and XML, allowing Web developers to create rich-featured event-driven Web applications that run on Web browsers. JavaScript is used to send XML-encoded data to server-side scripts (or to receive XML-encoded data from the server side) and to allow local, real-time page updates without the need to update 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.

   third, using XML in PHP

The available options for PHP interaction with XML have increased significantly since PHP 5.0. PHP version 4 provides an unstable and non-compliant DOM XML extension.
Below, I'll focus on the three methods PHP 5 provides to us that allow us to interact with XML: DOM, simple XML, and XPath. Where possible, I will recommend the conditions and data that are most appropriate for each method. All of the sample code uses 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>
  Iv. DOM

Dom PHP extensions allow the use of the world's Dom API to operate on XML documents. Before PHP 5 appeared, this was the only way PHP could access XML documents. If you use the DOM in JavaScript, you will realize that these object models are almost identical.

Because DOM methods are verbose when traversing and manipulating XML documents, any DOM-compatible code has obvious advantages-compatible with any other API that implements the same model of a compatible object.

In the example code below, we use the DOM to display information about each book. First, we walk through the list of directories, loading their IDs and their corresponding names into an indexed array. We then display a short 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) {
/* Notice how we get the attributes */
$cid = $categoryNode->getattribute (CID);
$categories [$cid] = $categoryNode->firstchild->nodevalue;
}
? >
<title> XML Library </title>
<body>
?
PHP foreach ($xml->getelementsbytagname (book) as $book):
/* Find Title */
$title = $book->getelementsbytagname (title)->item (0)->firstchild->nodevalue;
/* Find authors-for simplicity, we 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>

http://www.bkjia.com/PHPjc/508481.html www.bkjia.com true http://www.bkjia.com/PHPjc/508481.html techarticle XML Introduction XML (Extensible Markup Language) is a kind of standard, which is mainly used for easy interaction between Web application and server, storage and use of data. Use XML standard compilation ...

  • 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.