您的位置主页 > 编程专区 > Php > php实现将xml转换成数组array

php实现将xml转换成数组array

2011-06-01    文章来源:互联网    浏览次数:1092     分享文章

 

 
  1. //对象引用
  2. $t = new xml2array();
  3. $t->str = "kukaka.xml";
  4. $m = $t->xarray();
  5. echo "<pre>";
  6. print_r($m);
  7. echo "</pre>";
  8.  
  9. class xml2array {
  10.  
  11.     public $str = '';
  12.     public $type = 1; //0为字符串,1为文件
  13.  
  14.     function readxml()
  15.     {
  16.         if($this->type==1)
  17.         {
  18.             $this->xmlstr = simplexml_load_file($this->str);//simplexml_load_file()作用是:将一个XML文档装载入一个对象中。
  19.         }
  20.         else
  21.         {
  22.             $this->xmlstr = simplexml_load_string($this->str);
  23.         }
  24.     }
  25.     function xarray()
  26.     {
  27.         $this->readxml();
  28.         $arrstr = array();
  29.  
  30.         $str = serialize($this->xmlstr); //serialize()  产生一个可存储的值的表示 
  31.         $str = str_replace('O:16:"SimpleXMLElement"''a'$str);
  32.         $arrstr = unserialize($str); //unserialize()  从已存储的表示中创建 PHP 的值
  33.  
  34.         return $arrstr;
  35.     }
  36. }

 

文章评论(查看全部)

昵 称 *
电子邮箱 *
网 址      6 + 5 = ?