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

php 数组转xml的例子

发布:smiling 来源: PHP粉丝网  添加日期:2016-07-15 15:57:25 浏览: 评论:0 

数组转xml用到不多用数组转json比较多了,但今天有一个功能就是必须要把数组转换成xml了,下面小编人网上找到了一段关于php 数组转xml的代码测试了都可以使用,下面整理分享给各位。

例子1:

下面这个可以支持多维数组,测试代码:test.php

  1. <?php 
  2. include './ArrayToXML.php'; 
  3. header('Content-Type:   text/xml'); 
  4. $data=array("name"=>"zhangsan","sex"=>"0","address"=>array("sheng"=>"chongqing","shi"=>"nanchuan","zhen"=>"daguan")); 
  5. echo ArrayToXML::toXml($data); 
  6. ?> 

处理代码:ArrayToXML.php

  1. <?php 
  2. class ArrayToXML 
  3. { 
  4.  /** 
  5.   * The main function for converting to an XML document. 
  6.   * Pass in a multi dimensional array and this recrusively 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 - 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 wobbly if you don't. 
  16.   if (ini_get('zend.ze1_compatibility_mode') == 1) 
  17.   { 
  18.    ini_set ('zend.ze1_compatibility_mode', 0); 
  19.   } 
  20.   if ($xml == null) 
  21.   { 
  22.    $xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$rootNodeName />"); 
  23.   } 
  24.   // loop through the data passed in. 
  25.   foreach($data as $key => $value) 
  26.   { 
  27.    // no numeric keys in our xml please! 
  28.    if (is_numeric($key)) 
  29.    { 
  30.     // make string key... 
  31.     $key = "unknownNode_". (string) $key; 
  32.    } 
  33.    // replace anything not alpha numeric 
  34.    $key = preg_replace('/[^a-z]/i', '', $key); 
  35.    // if there is another array found recrusively call this function 
  36.    if (is_array($value)) 
  37.    { 
  38.     $node = $xml->addChild($key); 
  39.     // recrusive call. 
  40.     ArrayToXML::toXml($value, $rootNodeName, $node); 
  41.    } //phpfensi.com 
  42.    else 
  43.    { 
  44.     // add single node. 
  45.     $value = htmlentities($value); 
  46.     $xml->addChild($key,$value); 
  47.    } 
  48.   } 
  49.   // pass back as string. or simple xml object if you want! 
  50.   return $xml->asXML(); 
  51.  } 
  52. } 

ArrayToXML.php来源于网络,本人不做任何解释.

例子2:

  1. // Xml 转 数组, 包括根键,忽略空元素和属性,尚有重大错误 
  2. function xml_to_array( $xml ) 
  3. { 
  4.     $reg = "/<(\\w+)[^>]*?>([\\x00-\\xFF]*?)<\\/\\1>/"; 
  5.     if(preg_match_all($reg, $xml, $matches)) 
  6.     { 
  7.         $count = count($matches[0]); 
  8.         $arr = array(); 
  9.         for($i = 0; $i < $count; $i++) 
  10.         { 
  11.             $key= $matches[1][$i]; 
  12.             $val = xml_to_array( $matches[2][$i] );  // 递归 
  13.             if(array_key_exists($key, $arr)) 
  14.             { 
  15.                 if(is_array($arr[$key])) 
  16.                 { 
  17.                     if(!array_key_exists(0,$arr[$key])) 
  18.                     { 
  19.                         $arr[$key] = array($arr[$key]); 
  20.                     } 
  21.                 }else{ 
  22.                     $arr[$key] = array($arr[$key]); 
  23.                 } 
  24.                 $arr[$key][] = $val; 
  25.             }else{ 
  26.                 $arr[$key] = $val; 
  27.             } 
  28.         } 
  29.         return $arr; 
  30.     }else{ 
  31.         return $xml; 
  32.     } 
  33. } 
  34. // Xml 转 数组, 不包括根键 
  35. function xmltoarray( $xml ) 
  36. { 
  37.     $arr = xml_to_array($xml); 
  38.     $key = array_keys($arr); 
  39.     return $arr[$key[0]]; 
  40. } 
  41. // 类似 XPATH 的数组选择器 
  42. function xml_array_select( $arr, $arrpath ) 
  43. { 
  44.     $arrpath = trim( $arrpath, '/' ); 
  45.     if(!$arrpath) return $arr; 
  46.     $self = 'xml_array_select'; 
  47.      
  48.     $pos = strpos( $arrpath, '/' ); 
  49.     $pos = $pos ? $pos : strlen($arrpath); 
  50.     $curpath = substr($arrpath, 0, $pos); 
  51.     $next = substr($arrpath, $pos); 
  52.      
  53.     if(preg_match("/\\[(\\d+)\\]$/",$curpath,$predicate)) 
  54.     { 
  55.         $curpath = substr($curpath, 0, strpos($curpath,"[{$predicate[1]}]")); 
  56.         $result = $arr[$curpath][$predicate[1]]; 
  57.     }else $result = $arr[$curpath]; 
  58.      
  59.     if( is_array($arr) && !array_key_exists($curpath, $arr) ) 
  60.     { 
  61.         die( 'key is not exists:' . $curpath ); 
  62.     } 
  63.      
  64.     return $self($result, $next); 
  65. } 
  66. // 如果输入的数组是全数字键,则将元素值依次传输到 $callback, 否则将自身传输给$callback 
  67. function xml_array_each( $arr, $callback ) 
  68. { 
  69.     if(func_num_args()<2) die('parameters error'); 
  70.     if(!is_array($arr)) die('parameter 1 shuld be an array!'); 
  71.     if(!is_callable($callback)) die('parameter 2 shuld be an function!'); 
  72.     $keys = array_keys($arr); 
  73.     $isok = true; 
  74.     foreach( $keys as $key ) {if(!is_int($key)) {$isok = false; break;}} 
  75.     if($isok) 
  76.         foreach( $arr as $val ) $result[] = $callback($val); 
  77.     else 
  78.         $result[] = $callback( $arr ); 
  79.     return $result; 
  80. } 
  81. /** 
  82.  * 最简单的XML转数组 
  83.  * @param string $xmlstring XML字符串 
  84.  * @return array XML数组 
  85.  */ 
  86. function simplest_xml_to_array($xmlstring) { 
  87.     return json_decode(json_encode((array) simplexml_load_string($xmlstring)), true); 
  88. } 

Tags: 数组 例子

分享到: