I. Introduction to XML
XML (extensible Annotation language) is a standard of the web, which is mainly used for easy interaction, storage and use of data between web-based applications and servers.
Data encoded using XML standard has the meaning and structure that can be easily interpreted by human and computer. 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 connections and the need for consumer applications to share data across any medium means that XML Web services and applications are becoming richer.
The invention of XML is designed to solve the organizational problem that describes the rich data on the web, and so far this problem can only be partially solved through the ingenious use of HTML.
The following is an example of an XML document:
Program code
<?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>
If you've never seen XML before, then you can think of it as HTML. HTML is a SGML application, and XML is a subset of it. However, their similarity also includes a similar notation separator.
Just look at the XML fragment above and we can see that the data is a description of a party with some guests, each of which is corresponding to one item. The label name used to describe the data is entirely selected by the author. All XML standards require that the data must be consistent and used to describe the label of the data as well-formed. We can further enforce the integrity of the data with a document type declaration (DTD) or an XML schema. For simplicity's sake, however, we'll use only plain XML in this article.
Second, XML applications
Just now, we've seen how to use XML to describe any kind of data. In fact, XML has been widely used in many Web applications today, and the following are some of the most famous application descriptions:
· Xhtml-This is one of the most widely used XML applications. It is similar to an html-based sgml-used to describe how data is displayed on a Web page. XHTML uses a DTD to ensure that all documents conform to standards. The advent of XHTML has made web programmers a little easier to develop; however, a Web browser that is completely compatible with CSS and XHTML standards has not yet emerged.
· xml-rpc-Remote Procedure Call (RPC), applied to a distributed application to invoke a procedure on a remote computer. XML-RPC encodes the information about a procedure call by using it 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 with an HTTP connection.
· rss-Really Simple aggregation/rich Site Summary, which is used to aggregate Web site content (such as news, articles, shared prices and links, etc.), it uses a special application (an aggregator) to regularly update the RSS feed on the user's PC. The RSS data is encoded and transmitted using XML.
· ajax-asynchronous JavaScript and XML, allowing Web developers to create rich-feature event-driven Web applications that run on a Web browser. Where 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 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.
iii. using XML in PHP
Since PHP 5.0, PHP has significantly increased the available options for interacting with XML. PHP version 4 provides an unstable and non-consortium-compliant DOM XML extension.
Next, I'll focus on the three methods that PHP 5 gives us that allow us to interact with XML: DOM, simple XML, and XPath. Where possible, I will recommend the conditions and data that are best suited to each method. All of the sample code will use an XML data source to describe a library and the books it contains.
Program code
<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>
Four, DOM
The DOM PHP extension allows you to operate on an XML document using the World Service DOM API. This is the only way PHP can access an XML document before the advent of PHP 5. If you use DOM in JavaScript, you will realize that these object models are almost the same.
Because DOM methods are verbose in traversing and manipulating XML documents, any DOM-compliant code has a distinct advantage-compatible with any other API that implements the same common-sense-compatibility object model.
In the example code below, we use the DOM to display information about each book. First, we iterate through the list directories, loading their IDs and their corresponding names into an array of indices. Then we show a short description of each book:
Php:
<?php
/* Here we must specify the XML version: It is also 1.0 */
$xml = new DOMDocument (' 1.0 ');
$xml->load (' xml/library.xml ');
/* First, create a directory listing * *
$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 the sake of 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>
<p><b>author:</b>: <?php Echo ($author)?></p>
<p><b>categories: </b>: <?php Echo ($catList)?></p>
</div>
? PHP Endforeach;?>
[HTML]
To put it another way, it's more cumbersome to modify XML. For example, the code to add a directory is as follows:
Php:
[Code]
function Addcategory (DOMDocument $xml, $catID, $catName) {
$catName = $xml->createtextnode ($catName); Create a node to store text
$category = $xml->createelement (' category '); Create a directory element
$category->appendchild ($catName); Add text to a table of contents element
$category->setattribute (' CID ', $catID); Set the ID of the directory
$XMLCategories = $xml->getelementsbytagname (' categories ')->item (0);
$XMLCategories->appendchild ($category); Add New Directory
}
Five, save XML
You can use one of the Save () and SaveXML () methods to convert the DOM description back to the XML string description. The Save () method saves the XML to a file with a specified name, and SaveXML () returns a string from part or whole of the document.
$xml->save (' xml/library.xml ');
Save All Files
$categories = $xml->savexml ($XMLCategories);
Returns a string containing the kind
To illustrate how easy it is to migrate DOM-compliant code to another language, the following is the same code that is implemented in javascript:
Javascript:
function Doxml () {
/* First create a list of categories * *
var categories = Array ();
var xmlcategories = xml.getelementsbytagname (' categories ') [0];
var thecategories = xmlcategories.getelementsbytagname (' category ');
for (var i = 0; i < thecategories.length; i++) {
/* Notice how we get the attributes * *
var cid = Thecategories[i].getattribute (' cid ');
CATEGORIES[CID] = Thecategories[i].firstchild.nodevalue;
}
var thebooks = xml.getelementsbytagname (' book ');
for (var i = 0; i < thebooks.length; i++) {
var book = thebooks[i];
/* Find Title * *
var title = Book.getelementsbytagname (' title ') [0].firstchild.nodevalue;
/* Find author-For simplicity's sake, we assume that only one author * *
var author = book.getelementsbytagname (' author ') [0].firstchild.nodevalue;
/* List of types * *
var bookcategories = book.getelementsbytagname (' category ');
var catlist = ';
for (var j = 0; J < Bookcategories.length; J + +) {
Catlist + = Categories[bookcategories[j].firstchild.nodevalue] + ', ';
}
catlist = catlist.substring (0, catlist.length-2);
Document.open ();
document.write ("document.write ("<P><B>AUTHOR:</B>:" + Author + "</p>");
document.write ("<p><b>categories: </b>:" + catlist + "</p>");
}
Document.close ();
}
VI. Simple XML
Simple XML is really simple. It allows the use of object and array access methods to access an XML document and its elements and attributes. The operation is simple:
· Elements-these are described as individual properties of the SimpleXMLElement object. When more than one child element exists as a document or element, each element can be accessed using an array index flag.
$xml->books;//return element "books"
$xml->books->book[0];//Returns the first book in the books element
· Attributes-The attributes of an element are accessed and set by associative array flags, at which point each index corresponds to a property name.
$category [' CID '];//returns the value of the CID attribute
· element data-In order to retrieve text data contained within an element, it must be explicitly converted to a string by using (string) or output using print or echo. If an element contains multiple text nodes, they are connected in the order in which they are found.
Echo ($xml->books->book[0]->title);//show the title of the first book
The following is the original instance of converting using simple XML. To load an XML file, we use the simplexml_load_file () function to parse the XML file and load it into a SimpleXMLElement object:
Php:
<?php
$xml = simplexml_load_file (' Xml/library.xml ');
/* Load a list of directories into an array
$categories = Array ();
foreach ($xml->categories->category as $category) {
$categories [(String) $category [' cid ']] = (string) $category;
}
?>
<title>xml library</title>
<body>
<?php foreach ($xml->books->book as $book):
/* Listing Directory * *
$catList = ';
foreach ($book->category as $category) {
$catList. = $categories [((String) $category)]. ', ';
}
$catList = substr ($catList, 0,-2);?>
<div>
<p><b>author:</b>: <?php Echo ($book->author)?></p>
<p><b>categories: </b> PHP echo ($catList)?></p>
</div>
? PHP Endforeach;?>
Vii. Modifying XML
Although text data and property values can be set by using simple XML, you cannot create new objects. However, SIMPLEXM does provide a way to implement transitions between DomElement objects and DomElement objects. To do this, I modified the addcategory () function to illustrate how to use the Simplexml_import_dom () function to add a directory and convert the document back to a simple XML format:
Php:
function Addcategory (simplexmlelement & $sXML, $catID, $catName) {
$xml = new DOMDocument;
$xml->loadxml ($sXML->asxml ());
$catName = $xml->createtextnode ($catName); Create a node to store the text
$category = $xml->createelement (' category '); Create a directory element
$category->appendchild ($catName); Add text to a catalog element
$category->setattribute (' CID ', $catID); Set Directory ID
$XMLCategories = $xml->getelementsbytagname (' categories ')->item (0);
$XMLCategories->appendchild ($category); Add New Directory
$sXML = Simplexml_import_dom ($xml);
return $sXML;
}
Similarly, the Asxml () function of the SimpleXMLElement object can be used to retrieve an XML string and save it back to a file.
Eight, XPath
There is no doubt that XPath is "cherry on top of XML cake". XPath allows you to use SQL-like queries to find specific information in an XML document. Both DOM and SimpleXML have built-in support for XPath, such as SQL, that can be used to extract any content you want to extract from an XML document.
Program code
· category-find any category that appears in the document.
· /library/books-Find books for all children appearing as a library
· /library/categories/category[@cid]-to find all category that appear as library/categories and attribute CID.
· /library/categories/category[@att = ' 2 ']-look for all children who are library/categories and have attribute CID with a value of 2 appearing category.
· /library/books/book[title= ' Apache 2 ']-finds all children as/library/books and has a book with a value of Apache 2 appearing in the header element.
Actually, this is just one corner of the XPath iceberg. You can use XPath to create a large number of complex queries to extract almost any information from your document. I modified the sample code again to show you how easy it is to use XPath.
Php:
<?php
$xml = simplexml_load_file (' Xml/library.xml ');
?>
<title>xml library</title>
<body>
<?php foreach ((array) $xml->xpath ("/library/books/book")) as $book):
/* List Directory * *
$catList = ';
foreach ($book->category as $category) {
/* Get the directory with this ID * *
$category = $xml->xpath ("/library/categories/category[@cid = ' $category ')");
$catList. = (string) $category [0]. ', ';
}
$catList = substr ($catList, 0,-2);?>
<div>
<p><b>author:</b>: <?php Echo ($book->author)?></p>
<p><b>categories: </b>: <?php Echo ($catList)?></p>
</div>
<?php Endforeach;?>
IX, Dom and XPath
Evaluating an XPath query in the DOM requires creating a Domxpath object, and the following evaluate () function returns an array of DomElement.
$xPath = new Domxpath ($xml);
$xPath->evaluate ("/library/books/book[title= ' Apache 2 ']");
10. Conclusion
Now we've learned how to use the tools that PHP provides to us to interact with XML. So far, we've been "armed" and ready to delve into the XML application. In the next article, we'll discuss Ajax and how it can be applied to sites like Google.