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

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

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写入变量,最后输出。

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

xmlreader来读取xml数据
标签: xmlreader  读取  xml数据

代码片段(1)

[代码] [PHP]代码

01 xml源文件
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;
22 while ($reader->read()) {                                                              //是否读取
23 if ($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 :: TEXT XMLReader :: CDATA XMLReader :: WHITESPACE XMLReader :: 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.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值