如何正确运用PHP XMLReader解析XML文档

来源:互联网 发布:java ee源代码 编辑:程序博客网 时间:2024/04/29 13:09

我们在运用PHP语言进行实际编程中,经常会遇到需要对XML文档进行解析的需求。在PHP语言中提供了许多函数可以满足这一需求的实现。。其中PHP XMLReader循序地浏览过xml档案的节点,可以想像成游标走过整份文件的节点,并抓取需要的内容。

  • PHP mail()函数乱码的具体解决方法
  • 浅析多种PHP语法解析函数应当如何运用
  • 解读PHP DOMDocument在解析XML文件中的作
  • 详细解读PHP解析XML元素结构的代码示例
  • 深入探讨PHP命令行参数

PHP XMLReader的代码示例如下:

  1. < ?PHP  
  2. header("Content-type:text/html;
     Charset=utf-8");  
  3. $url = "http://www.google.com/
    ig/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 />';  

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

-------------------------------------

疯狂的流浪

php 读取xml的方法三---xmlreader来读取xml数据

疯狂的流浪 发布于 2010年12月22日 22时, 0评/639阅
分享到 
收藏+4
踩顶0
xmlreader来读取xml数据
标签:xmlreader 读取 xml数据

代码片段(1)

[代码] [PHP]代码

view source
print?
01xml源文件
02 
03<?xml version="1.0 encoding="UTF-8"?>
04  <humans>
05  <zhangying>
06  <name>张映</name>
07  <sex>男</sex>
08  <old>28</old>
09  </zhangying>
10  <tank>
11  <name>tank</name>
12  <sex>男</sex>
13  <old>28</old>
14  </tank>
15  </humans>
16 
17 
18<?php
19$reader new XMLReader();
20$reader->open('person.xml');                                                    //读取xml数据
21$i=1;
22while ($reader->read()) {                                                              //是否读取
23if ($reader->nodeType == XMLReader::TEXT) {               //判断node类型
24  if($i%3){
25   echo $reader->value;                                                                 //取得node的值
26  }else{
27   echo $reader->value."<br>" ;
28  }
29  $i++;
30}
31}
32?>------------------------------------------------------
libxml2 contains much more useful method readString() that will read and return whole text content of element. You can call it after receiving start tag (XMLReader::ELEMENT). You can use this PHP code to emulate this method until PHP will directly call underlying libxml2 implementation.

<?php
class XMLReader2 extends XMLReader
{
  function 
readString()
  {
       
$depth 1;
       
$text "";

       while (
$this->read() && $depth != 0)
       {
           if (
in_array($this->nodeType, array(XMLReader::TEXTXMLReader::CDATAXMLReader::WHITESPACEXMLReader::SIGNIFICANT_WHITESPACE)))
               
$text .= $this->value;
           if (
$this->nodeType == XMLReader::ELEMENT$depth++;
           if (
$this->nodeType == XMLReader::END_ELEMENT$depth--;
       }
       return 
$text;
   }
}
?>

Just use XMLReader2 instead of XMLReader.

原创粉丝点击