注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

isblog主页

或许不一定要做得最多,但一定要做得最好! 能力就是一种财富,积累越多,就越体现你

 
 
 

日志

 
 

xml转数组的方法  

2011-07-24 01:16:24|  分类: PHP学习 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
前言: 有时因项目需要,在调用别人接口时返回xml格式的内容,这时需要处理xml数据并写入数据库中,因个人技术有限开始很纠结,后面发现已有高手写出了怎么一个方法,看了很不错。(收藏了~~)
第一种简单方法:
/**  
* xml转数组方法 *  
* @param xml $xml xml格式字符串 
* @param boolean $recursive 是否解释一串XML入一个对象中  
* @return array  
*/ 
function XML2Array ( $xml , $recursive = false ) {  
if ( ! $recursive ){  
$tmpData = simplexml_load_string ( $xml ) ;  
}else{  
$tmpData = $xml ;  
}  
$newArray = array () ;  
foreach ( $tmpData as $key => $value ){  
$value = ( array ) $value ;  
if ( isset ( $value [ 0 ] ) && ! is_object ( $value [ 0 ] ) ){  
$newArray [ $key ] = trim ( $value [ 0 ] ) ;  
}else{  
$newArray [ $key ] = XML2Array ( $value , true ) ;  
}  
}  
return $newArray ;
}
解析xml格式如下:
$str=<<<XML
<?xml version="1.0" ?>
<library>
    <book>
        <ddd>swwss</ddd>
        <ddd id="y7" dd='yy'>
            <dd>ccc</dd>
        </ddd>
    </book>
    <return>
      <name>lin</name>
      <age>44</age>
    </return>
    <return>
      <name>lin2</name>
      <age>45</age>
    </return>
</library>
XML;

对如下格式解析不出属性:
<ddd id="y7" dd='yy'>swwss</ddd>

第二种复杂方法:
function simplexml4array($xmle){
  $haschildren=false;
  foreach($xmle->attributes() as $k=>$c)
  {
    $r[$xmle->getName()]['@att'][$k]=(string)$c;//保存属性值
  }
  foreach($xmle->children() as $k=>$c)
  {
    $haschildren=true;
    $res = simplexml4array($c);
    if(!empty($res)){
        $r[$xmle->getName()][]=$res;
    }
  }
  if(!$haschildren)
  {
      $str=(string)$xmle;
      if(!empty($str)){
          $r[$xmle->getName()]['@txt']=(string)$xmle;//保存文本值 
      }
  }
  return $r;
}
//附上一个测试的例子
$str=<<<XML
<?xml version="1.0" ?>
<library>
    <book>
        <ddd id="x6" dd="xx">swwss</ddd>
        <ddd id="y7" dd='yy'>
            <dd>ccc</dd>
        </ddd>
    </book>
    <return>
      <name>lin</name>
      <age>44</age>
    </return>
    <return>
      <name>lin2</name>
      <age>45</age>
    </return>
</library>
XML;

$xml = simplexml_load_string($str);
$arr = simplexml4array($xml);
print_r($arr);

下面是得到的数组,有些人认为数组里有下标[0]、[1]等之类的是多余的(开始我也怎么认为),后面经测试这样做是出现相同的标签时区分开多个。如测试的例子中就验证了这一点,因为有时xml要返回多条值,而为了得到这些值它们之间就需要使用下标来区分。
输出数组格式:
  • Array
  • (
  •     [library] => Array
  •         (
  •             [0] => Array
  •                 (
  •                     [book] => Array
  •                         (
  •                             [0] => Array
  •                                 (
  •                                     [ddd] => Array
  •                                         (
  •                                             [@att] => Array
  •                                                 (
  •                                                     [id] => x6
  •                                                     [dd] => xx
  •                                                 )
  •                                             [@txt] => swwss
  •                                         )
  •                                 )
  •                             [1] => Array
  •                                 (
  •                                     [ddd] => Array
  •                                         (
  •                                             [@att] => Array
  •                                                 (
  •                                                     [id] => y7
  •                                                     [dd] => yy
  •                                                 )
  •                                             [0] => Array
  •                                                 (
  •                                                     [dd] => Array
  •                                                         (
  •                                                             [@txt] => ccc
  •                                                         )
  •                                                 )
  •                                         )
  •                                 )
  •                         )
  •                 )
  •             [1] => Array
  •                 (
  •                     [return] => Array
  •                         (
  •                             [0] => Array
  •                                 (
  •                                     [name] => Array
  •                                         (
  •                                             [@txt] => lin
  •                                         )
  •                                 )
  •                             [1] => Array
  •                                 (
  •                                     [age] => Array
  •                                         (
  •                                             [@txt] => 44
  •                                         )
  •                                 )
  •                         )
  •                 )
  •             [2] => Array
  •                 (
  •                     [return] => Array
  •                         (
  •                             [0] => Array
  •                                 (
  •                                     [name] => Array
  •                                         (
  •                                             [@txt] => lin2
  •                                         )
  •                                 )
  •                             [1] => Array
  •                                 (
  •                                     [age] => Array
  •                                         (
  •                                             [@txt] => 45
  •                                         )
  •                                 )
  •                         )
  •                 )
  •         )
  • )

原文地址:http://bbs.chinaunix.net/thread-808855-1-1.html
  评论这张
 
阅读(805)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018