国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP數(shù)組與XML之間的轉(zhuǎn)換的例子

2024-09-04 11:49:17
字體:
供稿:網(wǎng)友

數(shù)組與XML方法有不少如直接使用遍歷數(shù)組然后生成xml同時也可以使用DOMDocument來生成xml,具體的方法與步驟如下所示。

PHP將數(shù)組轉(zhuǎn)換成XML:

PHP可以將數(shù)組轉(zhuǎn)換成xml格式,簡單的辦法是遍歷數(shù)組,然后將數(shù)組的key/value轉(zhuǎn)換成xml節(jié)點,再直接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

我測試了下,這個最簡單,速度又快,支持多為數(shù)組,中文也不會亂碼。

另一種方法是利用DOMDocument來生成xml結(jié)構(gòu),代碼如下:

  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.     }  //Vevb.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(); 

它同樣可以將數(shù)組轉(zhuǎn)換成xml,而且支持多維數(shù)組,生成的xml中文也不會亂碼。

PHP將XML轉(zhuǎn)換成數(shù)組:

做接口開發(fā)的時候經(jīng)常會碰到別人提交給你的是xml格式的數(shù)據(jù),常見的微信接口、支付寶接口等,他們的接口如發(fā)送消息通信都是xml格式的,那么我們先想辦法拿到這個xml數(shù)據(jù),然后再將其轉(zhuǎn)化成數(shù)組。

假設(shè)我們獲取到一個這樣的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數(shù)據(jù),然后先轉(zhuǎn)成json格式,再轉(zhuǎn)換成數(shù)組,代碼如下:

  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

得到數(shù)組后,我們就可以對數(shù)據(jù)進(jìn)行各種處理了。

下面是網(wǎng)上的,代碼如下:

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

下面是我自己編輯的代碼:

  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(); 

XML轉(zhuǎn)成數(shù)組,代碼如下,如果你使用 curl 獲取的 xml data.

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

如果是直接獲取 URL 數(shù)據(jù)的話:

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

先把 simplexml 對象轉(zhuǎn)換成 json,再將 json 轉(zhuǎn)換成數(shù)組。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 灵宝市| 沙洋县| 北京市| 定远县| 麦盖提县| 本溪| 乌拉特中旗| 库伦旗| 济宁市| 海城市| 贵溪市| 将乐县| 峡江县| 寻乌县| 屏南县| 浦县| 涟水县| 中阳县| 云阳县| 辽中县| 尼木县| 鹤岗市| 许昌市| 色达县| 崇文区| 中卫市| 屯留县| 江源县| 慈溪市| 玉田县| 噶尔县| 浙江省| 来凤县| 谢通门县| 工布江达县| 灵川县| 乡宁县| 左权县| 阿巴嘎旗| 青岛市| 大安市|