php 数组转xml的例子

数组转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. $pos = strpos( $arrpath, '/' );
  48. $pos = $pos ? $pos : strlen($arrpath);
  49. $curpath = substr($arrpath, 0, $pos);
  50. $next = substr($arrpath, $pos);
  51. if(preg_match("/\\[(\\d+)\\]$/",$curpath,$predicate))
  52. {
  53. $curpath = substr($curpath, 0, strpos($curpath,"[{$predicate[1]}]"));
  54. $result = $arr[$curpath][$predicate[1]];
  55. }else $result = $arr[$curpath];
  56. if( is_array($arr) && !array_key_exists($curpath, $arr) )
  57. {
  58. die( 'key is not exists:' . $curpath );
  59. }
  60. return $self($result, $next);
  61. }
  62. // 如果输入的数组是全数字键,则将元素值依次传输到 $callback, 否则将自身传输给$callback
  63. function xml_array_each( $arr, $callback )
  64. {
  65. if(func_num_args()<2) die('parameters error');
  66. if(!is_array($arr)) die('parameter 1 shuld be an array!');
  67. if(!is_callable($callback)) die('parameter 2 shuld be an function!');
  68. $keys = array_keys($arr);
  69. $isok = true;
  70. foreach( $keys as $key ) {if(!is_int($key)) {$isok = false; break;}}
  71. if($isok)
  72. foreach( $arr as $val ) $result[] = $callback($val);
  73. else
  74. $result[] = $callback( $arr );
  75. return $result;
  76. }
  77. /**
  78. * 最简单的XML转数组
  79. * @param string $xmlstring XML字符串
  80. * @return array XML数组
  81. */
  82. function simplest_xml_to_array($xmlstring) {
  83. return json_decode(json_encode((array) simplexml_load_string($xmlstring)), true);
  84. }