作者:傲气战歌网 来源:www.27zg.com 发表时间:2014-03-24 03:02
Introduction
This tutorial was originally posted on the old site on 20th December, 2007.
XML, Extensible Markup Language, is a general-purpose markup language which can be used for storing arbitrary data in a structured way. It is often used for sharing data between applications and a common usage of XML is for instance RSS feeds.
With the emerge of PHP 5, support for handling XML data has greatly improved and in this tutorial we will take a look at the features in PHP 5 which we can use to parse, alter and create XML documents. At the end of the tutorial we will also take a look at how you can create an RSS feed without writing a single line of XML and then we will make it into a reusable class which you can implement in your own applications. Additionally we'll create a really simple RSS reader.
Basic knowledge of XML is a prerequisite for this tutorial. Knowledge of XPath would be a good idea as it will be used a bit in this tutorial, however, this is not entirely important. Furthermore, OOP knowledge is a requirement as well.
Right, let's go to the next page and get started...
Parsing XMLPHP 5 has a class called SimpleXML which is... simple to use.
Throughout this tutorial we will use the file books.xml which I created using data from the top three books on Amazon.com's Editor's Picks: Best Books of 2007. The content of the file is:
<?xml version="1.0"?> <books> <book isbn="978-1594489501"> <title>A Thousand Splendid Suns</title> <author>Khaled Hosseini</author> <publisher>Riverhead Hardcover</publisher> <amazon_price>14.27</amazon_price> </book> <book isbn="978-1594489587"> <title>The Brief Wondrous Life of Oscar Wao</title> <author>Junot Diaz</author> <publisher>Riverhead Hardcover</publisher> <amazon_price>14.97</amazon_price> </book> <book isbn="978-0545010221"> <title>Harry Potter and the Deathly Hallows</title> <author>J. K. Rowling</author> <publisher>Arthur A. Levine Books</publisher> <amazon_price>19.24</amazon_price> </book> </books>We can load our data into SimpleXML in two ways. Either we can pass a string to SimpleXML or we can tell it where the file is:
<?php // Passing the XML $data = file_get_contents('books.xml'); $books = SimpleXMLElement($data); //------------------- // Passing a filename $books = SimpleXMLElement('books.xml', null, true); ?>If you have the file stored you should obviously use the latter approach as there is no reason to get the file contents ourselves when we can get SimpleXML to do it for us. The first way could be used if you already have some XML data which could for instance be retrieved from some web service. I only used file_get_contents() for the purpose of that example.
The second argument is used to set certain options. We won't touch that in this tutorial, but you are free to research that yourself. The third argument specifies if the first argument is a filename (true) or actual XML data (false) and defaults to false. If allow_url_fopen is set to true in php.ini then you can specify a URL as well.
Another way of loading a file is by using the simplexml_load_file() function. In that case it will look like this:
$books = simplexml_load_file('books.xml');Now let's turn our XML data into an HTML table:
<?php // load SimpleXML $books = new SimpleXMLElement('books.xml', null, true); echo <<<EOF <table> <tr> <th>Title</th> <th>Author</th> <th>Publisher</th> <th>Price at Amazon.com</th> <th>ISBN</th> </tr> EOF; foreach($books as $book) // loop through our books { echo <<<EOF <tr> <td>{$book->title}</td> <td>{$book->author}</td> <td>{$book->publisher}</td> <td>\${$book->amazon_price}</td> <td>{$book['isbn']}</td> </tr> EOF; } echo '</table>'; ?>This will result in a table looking like this:
<table> <tr> <th>Title</th> <th>Author</th> <th>Publisher</th> <th>Price at Amazon.com</th> <th>ISBN</th> </tr> <tr> <td>A Thousand Splendid Suns</td> <td>Khaled Hosseini</td> <td>Riverhead Hardcover</td> <td>$14.27</td> <td>978-1594489501</td> </tr> <tr> <td>The Brief Wondrous Life of Oscar Wao</td> <td>Junot Diaz</td> <td>Riverhead Hardcover</td> <td>$14.97</td> <td>978-1594489587</td> </tr> <tr> <td>Harry Potter and the Deathly Hallows</td> <td>J. K. Rowling</td> <td>Arthur A. Levine Books</td> <td>$19.24</td> <td>978-0545010221</td> </tr> </table>Right, so you say you wanted to display the title of the second book you'd do this (remember that array indices start from 0):
echo $books->book[1]->title;The ISBN number of the same book is displayed like this:
echo $books->book[1]['isbn'];XPath下一篇:常用RSS阅读器评测