找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 730|回复: 0

PHP中的XML解析的几种方法

[复制链接]

35

主题

9

回帖

214

积分

中级会员

积分
214
发表于 2022-3-9 09:28:51 | 显示全部楼层 |阅读模式
【使用DomDocument解析】

  1. <?PHP
  2. header("Content-type:text/html; Charset=utf-8");
  3. $url = "http://www.google.com/hhh/api?weather=shenzhen";
  4. // 加载XML内容
  5. $content = file_get_contents($url);
  6. $content = get_utf8_string($content);
  7. $dom = DOMDocument::loadXML($content);
  8. /*此处也可使用如下所示的代码,
  9.   $dom = new DOMDocument();
  10.   $dom->load($url);
  11. */
  12. $elements = $dom->getElementsByTagName("current_conditions");
  13. $element = $elements->item(0);
  14. $condition = get_google_xml_data($element, "condition");
  15. $temp_c = get_google_xml_data($element, "temp_c");
  16. echo '天气:', $condition, '<br />';
  17. echo '温度:', $temp_c, '<br />';

  18. function get_utf8_string($content) {
  19.    // 将一些字符转化成utf8格式
  20.    $encoding = mb_detect_encoding($content, array('ASCII','UTF-8','GB2312','GBK','BIG5'));
  21.    return mb_convert_encoding($content, 'utf-8', $encoding);
  22. }
  23. function get_google_xml_data($element, $tagname) {
  24.    $tags = $element->getElementsByTagName($tagname);
  25.    // 取得所有的
  26.    $tagname$tag = $tags->item(0);
  27.    // 获取第一个以$tagname命名的标签
  28.    if ($tag->hasAttributes()) {
  29.      // 获取data属性
  30.      $attribute = $tag->getAttribute("data");
  31.      return $attribute;
  32.    }else {
  33.      return false;
  34.    }
  35. }
复制代码

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

  1. <?php
  2. header("Content-type:text/html; Charset=utf-8");
  3. $url = "http://www.google.com/hhh/api?weather=shenzhen";
  4. // 加载XML内容
  5. $xml = new XMLReader();
  6. $xml->open($url);
  7. $condition = '';
  8. $temp_c = '';
  9. while ($xml->read()) {
  10.   // echo $xml->name, "==>", $xml->depth, "<br>";
  11.   if (!empty($condition) && !empty($temp_c)) {
  12.     break;
  13.   }
  14.   if ($xml->name == 'condition' && empty($condition)) {
  15.      // 取第一个condition
  16.      $condition = $xml->getAttribute('data');
  17.    }
  18.    if ($xml->name == 'temp_c' && empty($temp_c)) {
  19.      // 取第一个temp_c
  20.      $temp_c = $xml->getAttribute('data');
  21.    }
  22.    $xml->read();
  23. }
  24. $xml->close();
  25. echo '天气:', $condition, '<br />';
  26. echo '温度:', $temp_c, '<br />';
复制代码

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

  1. <?php
  2. header("Content-type:text/html; Charset=utf-8");
  3. $url = "http://www.google.com/hhh/api?weather=shenzhen";
  4. // 加载XML内容
  5. $dom = new DOMDocument();
  6. $dom->load($url);
  7. $xpath = new DOMXPath($dom);
  8. $element = $xpath->query("/xml_api_reply/weather/current_conditions")->item(0);
  9. $condition = get_google_xml_data($element, "condition");
  10. $temp_c = get_google_xml_data($element, "temp_c");
  11. echo '天气:', $condition, '<br />';
  12. echo '温度:', $temp_c, '<br />';

  13. function get_google_xml_data($element, $tagname) {
  14.   $tags = $element->getElementsByTagName($tagname);
  15.   // 取得所有的$tagname
  16.   $tag = $tags->item(0);
  17.   // 获取第一个以$tagname命名的标签
  18.   if ($tag->hasAttributes()) {
  19.     // 获取data属性
  20.     $attribute = $tag->getAttribute("data");
  21.     return $attribute;
  22.   }else {
  23.     return false;
  24.   }
  25. }
复制代码








回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|魅力松滋 ( 鄂ICP备2024076975号-1 )

GMT+8, 2025-5-6 23:08 , Processed in 0.047785 second(s), 17 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表