听dj战歌,就上傲气战歌网!2015年传奇家族玩家最喜爱的家族战歌网
战歌推荐:战歌网 战歌网dj Mc战歌网 DJ战歌网下载 激情战歌-冰雪战歌网 客服Q:350317
新闻搜索:

Handling XML data

作者:傲气战歌网     来源: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 XML

PHP 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

数据统计中!!

最新评论共有  位网友发表了评论
发表评论(评论内容:请文明参与评论,禁止谩骂攻击!)
不能超过250字节,请自觉遵守互联网相关政策法规.
昵称:    发表评论 (Ctrl+Enter快速回复)

关于本站 | 合作加盟 | 合作说明 | 免责声明 | 广告服务 | 网站地图

健康游戏忠告:抵制不良游戏 拒绝盗版游戏 注意自我保护 谨防受骗上当 适度游戏益脑 沉迷游戏伤身 合理安排时间 享受健康生活

如有意见和建议,请惠赐E-mail至350317@qq.com 联系QQ:350317

Copyright © 2010-2013 Www.27zG.CoM
苏ICP备11049833号