当前位置:首页 > PHP教程 > php函数 > 列表

PHP数组与XML之间的转换的例子

发布:smiling 来源: PHP粉丝网  添加日期:2016-07-27 13:17:59 浏览: 评论:0 

数组与XML方法有不少如直接使用遍历数组然后生成xml同时也可以使用DOMDocument来生成xml,具体的方法与步骤如下所示。

PHP将数组转换成XML:

PHP可以将数组转换成xml格式,简单的办法是遍历数组,然后将数组的key/value转换成xml节点,再直接echo输出了,如:

  1. function arrayToXml($arr){ 
  2.     $xml = "<root>"; 
  3.     foreach ($arr as $key=>$val){ 
  4.         if(is_array($val)){ 
  5.             $xml.="<".$key.">".arrayToXml($val)."</".$key.">"; 
  6.         }else{ 
  7.             $xml.="<".$key.">".$val."</".$key.">"; 
  8.         } 
  9.     } 
  10.     $xml.="</root>"; 
  11.     return $xml; 
  12. } 

我测试了下,这个最简单,速度又快,支持多为数组,中文也不会乱码。

另一种方法是利用DOMDocument来生成xml结构,代码如下:

  1. function arrayToXml($arr,$dom=0,$item=0){ 
  2.     if (!$dom){ 
  3.         $dom = new DOMDocument("1.0"); 
  4.     } 
  5.     if(!$item){ 
  6.         $item = $dom->createElement("root"); 
  7.         $dom->appendChild($item); 
  8.     }  //phpfensi.com 
  9.     foreach ($arr as $key=>$val){ 
  10.         $itemx = $dom->createElement(is_string($key)?$key:"item"); 
  11.         $item->appendChild($itemx); 
  12.         if (!is_array($val)){ 
  13.             $text = $dom->createTextNode($val); 
  14.             $itemx->appendChild($text); 
  15.              
  16.         }else { 
  17.             arrayToXml($val,$dom,$itemx); 
  18.         } 
  19.     } 
  20.     return $dom->saveXML(); 
  21. } 

它同样可以将数组转换成xml,而且支持多维数组,生成的xml中文也不会乱码。

PHP将XML转换成数组:

做接口开发的时候经常会碰到别人提交给你的是xml格式的数据,常见的微信接口、支付宝接口等,他们的接口如发送消息通信都是xml格式的,那么我们先想办法拿到这个xml数据,然后再将其转化成数组。

假设我们获取到一个这样的XML,代码如下:

  1. <root> 
  2. <user>月光光abcd</user> 
  3. <pvs>13002</pvs> 
  4. <ips> 
  5. <baidu_ip>1200</baidu_ip> 
  6. <google_ip>1829</google_ip> 
  7. </ips> 
  8. <date>2016-06-01</date> 
  9. </root> 

通过simplexml_load_string()解析读取xml数据,然后先转成json格式,再转换成数组,代码如下:

  1. function xmlToArray($xml){     
  2.     //禁止引用外部xml实体 
  3.     libxml_disable_entity_loader(true); 
  4.     $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); 
  5.     $val = json_decode(json_encode($xmlstring),true);   
  6.     return $val; 
  7. } 

得到数组后,我们就可以对数据进行各种处理了。

下面是网上的,代码如下:

  1. class ArrayToXML 
  2. { 
  3.     /** 
  4.      * The main function for converting to an XML document. 
  5.      * Pass in a multi dimensional array and this recrusively 
  6. loops through and builds up an XML document. 
  7.      * 
  8.      * @param array $data 
  9.      * @param string $rootNodeName - what you want the root node to be
  10.  - defaultsto data. 
  11.      * @param SimpleXMLElement $xml - should only be used recursively 
  12.      * @return string XML 
  13.      */ 
  14.     public static function toXml($data, $rootNodeName = 'data', $xml=null) 
  15.     { 
  16.         // turn off compatibility mode as simple xml throws a 
  17. wobbly if you don't. 
  18.         if (ini_get('zend.ze1_compatibility_mode') == 1) 
  19.         { 
  20.             ini_set ('zend.ze1_compatibility_mode', 0); 
  21.         } 
  22.         
  23.         if ($xml == null) 
  24.         { 
  25.             $xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$rootNodeName />"); 
  26.         } 
  27.         
  28.         // loop through the data passed in. 
  29.         foreach($data as $key => $value) 
  30.         { 
  31.             // no numeric keys in our xml please! 
  32.             if (is_numeric($key)) 
  33.             { 
  34.                 // make string key... 
  35.                 $key = "unknownNode_". (string) $key; 
  36.             } 
  37.             
  38.             // replace anything not alpha numeric 
  39.             $key = preg_replace('/[^a-z]/i', '', $key); 
  40.             
  41.             // if there is another array found recrusively call this function 
  42.             if (is_array($value)) 
  43.             { 
  44.                 $node = $xml->addChild($key); 
  45.                 // recrusive call. 
  46.                 ArrayToXML::toXml($value, $rootNodeName, $node); 
  47.             } //phpfensi.com 
  48.             else 
  49.             { 
  50.                 // add single node. 
  51.                                 $value = htmlentities($value); 
  52.                 $xml->addChild($key,$value); 
  53.             } 
  54.             
  55.         } 
  56.         // pass back as string. or simple xml object if you want! 
  57.         return $xml->asXML(); 
  58.     } 
  59. } 

下面是我自己编辑的代码:

  1. function arrtoxml($arr,$dom=0,$item=0){ 
  2.     if (!$dom){ 
  3.         $dom = new DOMDocument("1.0"); 
  4.     } 
  5.     if(!$item){ 
  6.         $item = $dom->createElement("root"); 
  7.         $dom->appendChild($item); 
  8.     } 
  9.     foreach ($arr as $key=>$val){ 
  10.         $itemx = $dom->createElement(is_string($key)?$key:"item"); 
  11.         $item->appendChild($itemx); 
  12.         if (!is_array($val)){ 
  13.             $text = $dom->createTextNode($val); 
  14.             $itemx->appendChild($text); 
  15.             
  16.         }else { 
  17.             arrtoxml($val,$dom,$itemx); 
  18.         } 
  19.     } 
  20.     return $dom->saveXML(); 
  21. } 

XML转成数组,代码如下,如果你使用 curl 获取的 xml data.

  1. $xml = simplexml_load_string($data); 
  2. $data['tk'] = json_decode(json_encode($xml),TRUE); 

如果是直接获取 URL 数据的话:

  1. $xml = simplexml_load_file($data); 
  2. $data['tk'] = json_decode(json_encode($xml),TRUE); 

先把 simplexml 对象转换成 json,再将 json 转换成数组。

Tags: PHP数组 XML转换

分享到: