免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 8171 | 回复: 1
打印 上一主题 下一主题

PHP数组转成XML [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-08-08 13:03 |只看该作者 |倒序浏览
PHP数组转成XML


网上找的一段代码! 然后我自己更具php DOMDocument又写了一段代码!~~

下面是网上的

  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 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 - defaultsto data.
  9.      * @param SimpleXMLElement $xml - should only be used recursively
  10.      * @return string XML
  11.      */
  12.     public static function toXml($data, $rootNodeName = 'data', $xml=null)
  13.     {
  14.         // turn off compatibility mode as simple xml throws a wobbly if you don't.
  15.         if (ini_get('zend.ze1_compatibility_mode') == 1)
  16.         {
  17.             ini_set ('zend.ze1_compatibility_mode', 0);
  18.         }
  19.         
  20.         if ($xml == null)
  21.         {
  22.             $xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$rootNodeName />");
  23.         }
  24.         
  25.         // loop through the data passed in.
  26.         foreach($data as $key => $value)
  27.         {
  28.             // no numeric keys in our xml please!
  29.             if (is_numeric($key))
  30.             {
  31.                 // make string key...
  32.                 $key = "unknownNode_". (string) $key;
  33.             }
  34.             
  35.             // replace anything not alpha numeric
  36.             $key = preg_replace('/[^a-z]/i', '', $key);
  37.             
  38.             // if there is another array found recrusively call this function
  39.             if (is_array($value))
  40.             {
  41.                 $node = $xml->addChild($key);
  42.                 // recrusive call.
  43.                 ArrayToXML::toXml($value, $rootNodeName, $node);
  44.             }
  45.             else
  46.             {
  47.                 // add single node.
  48.                                 $value = htmlentities($value);
  49.                 $xml->addChild($key,$value);
  50.             }
  51.             
  52.         }
  53.         // pass back as string. or simple xml object if you want!
  54.         return $xml->asXML();
  55.     }
  56. }
复制代码
下面是我自己编辑的代码


  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. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2013-09-14 15:34 |只看该作者
来晚了,但是还是分享一下我处理 PHP 数组与 XML 互转的类库
http://www.bacysoft.cn/thread-91-1-1.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP