网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

PHP解析XML的几种方法(附代码)

不管是桌面软件开发,还是WEB应用,XML无处不在!

创新互联专业为企业提供蔚县网站建设、蔚县做网站、蔚县网站设计、蔚县网站制作等企业网站建设、网页设计与制作、蔚县企业网站模板建站服务,十年蔚县做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

然而在平时的工作中,仅仅是使用一些已经封装好的类对XML对于处理,包括生成,解析等。假期有空,于是将PHP中的几种XML解析方法总结如下:

以解析Google API 接口提供的天气情况为例,我们取今天的天气及气温。

API地址:http://www.google.com/ig/api?weather=shenzhen

【XML文件内容】

  
                                                                                                                          

【使用DomDocument解析】

load($url);
 */
 
$elements = $dom->getElementsByTagName("current_conditions");
$element = $elements->item(0);
$condition = get_google_xml_data($element, "condition");
$temp_c = get_google_xml_data($element, "temp_c");
echo '天气:', $condition, '
'; echo '温度:', $temp_c, '
'; function get_utf8_string($content) { // 将一些字符转化成utf8格式 $encoding = mb_detect_encoding($content, array('ASCII','UTF-8','GB2312','GBK','BIG5')); return mb_convert_encoding($content, 'utf-8', $encoding); } function get_google_xml_data($element, $tagname) { $tags = $element->getElementsByTagName($tagname); // 取得所有的$tagname $tag = $tags->item(0); // 获取第一个以$tagname命名的标签 if ($tag->hasAttributes()) { // 获取data属性 $attribute = $tag->getAttribute("data"); return $attribute; }else { return false; } } ?>

这只是一个简单的示例,仅包括了loadXML, item, getAttribute,getElementsByTagName等方法,还有一些有用的方法,这个依据你的实际需要。

【XMLReader】

当我们要用php解读xml的内容时,有很多物件提供函式,让我们不用一个一个字元去解析,而只要根据标签和属性名称,就能取出文件中的属性与内容了,相较之下方便许多。其中XMLReader循序地浏览过xml档案的节点,可以想像成游标走过整份文件的节点,并抓取需要的内容。

open($url);
 
$condition = '';
$temp_c = '';
while ($xml->read()) {
//      echo $xml->name, "==>", $xml->depth, "
"; if (!empty($condition) && !empty($temp_c)) { break; } if ($xml->name == 'condition' && empty($condition)) { // 取第一个condition $condition = $xml->getAttribute('data'); } if ($xml->name == 'temp_c' && empty($temp_c)) { // 取第一个temp_c $temp_c = $xml->getAttribute('data'); } $xml->read(); } $xml->close(); echo '天气:', $condition, '
'; echo '温度:', $temp_c, '
';

我们只是需要取第一个condition和第一个temp_c,于是遍历所有的节点,将遇到的第一个condition和第一个temp_c写入变量,最后输出。

【DOMXPath】

这种方法需要使用DOMDocument对象创建整个文档的结构,

load($url);
 
$xpath = new DOMXPath($dom);
$element = $xpath->query("/xml_api_reply/weather/current_conditions")->item(0);
$condition = get_google_xml_data($element, "condition");
$temp_c = get_google_xml_data($element, "temp_c");
echo '天气:', $condition, '
'; echo '温度:', $temp_c, '
'; function get_google_xml_data($element, $tagname) { $tags = $element->getElementsByTagName($tagname); // 取得所有的$tagname $tag = $tags->item(0); // 获取第一个以$tagname命名的标签 if ($tag->hasAttributes()) { // 获取data属性 $attribute = $tag->getAttribute("data"); return $attribute; }else { return false; } } ?>

【xml_parse_into_struct】

说明:int xml_parse_into_struct ( resource parser, string data, array &values [, array &index] )

该函数将 XML 文件解析到两个对应的数组中,index 参数含有指向 values 数组中对应值的指针。最后两个数组参数可由指针传递给函数。

注意: xml_parse_into_struct() 失败返回 0,成功返回 1。这和 FALSE 与 TRUE 不同,使用例如 === 的运算符时要注意。

';
echo '温度:', $vals[$index['TEMP_C'][0]]['attributes']['DATA'], '
';

【Simplexml】

此方法在PHP5中可用

这个在google的官方文档中有相关的例子,如下:

// Charset: utf-8
/**
  * 用php Simplexml 调用google天气预报api,和g官方的例子不一样
  * google 官方php domxml 获取google天气预报的例子
  * http://www.google.com/tools/toolbar/buttons/intl/zh-CN/apis/howto_guide.html
  *
  * @copyright Copyright (c) 2008 
  * @license New BSD License
  * @version 2008-11-9
  */
 
// 城市,用城市拼音
$city = empty($_GET['city']) ? 'shenzhen' : $_GET['city'];
$content = file_get_contents("http://www.google.com/ig/api?weather=$city&hl=zh-cn");
$content || die("No such city's data");
$content = mb_convert_encoding($content, 'UTF-8', 'GBK');
$xml = simplexml_load_string($content);
 
$date = $xml->weather->forecast_information->forecast_date->attributes();
$html = $date. "
\\r\\n"; $current = $xml->weather->current_conditions; $condition = $current->condition->attributes(); $temp_c = $current->temp_c->attributes(); $humidity = $current->humidity->attributes(); $icon = $current->icon->attributes(); $wind = $current->wind_condition->attributes(); $condition && $condition = $xml->weather->forecast_conditions->condition->attributes(); $icon && $icon = $xml->weather->forecast_conditions->icon->attributes(); $html.= "当前: {$condition}, {$temp_c}°C, {$humidity} {$wind}
\\r\\n"; foreach($xml->weather->forecast_conditions as $forecast) { $low = $forecast->low->attributes(); $high = $forecast->high->attributes(); $icon = $forecast->icon->attributes(); $condition = $forecast->condition->attributes(); $day_of_week = $forecast->day_of_week->attributes(); $html.= "{$day_of_week} : {$high} / {$low} °C, {$condition}
\\r\\n"; } header('Content-type: text/html; Charset: utf-8'); print $html; ?>

相关推荐:PHP教程
分享文章:PHP解析XML的几种方法(附代码)
文章路径:http://bjjierui.cn/article/cphjps.html

其他资讯