博客
关于我
array与xml转换实现(转)
阅读量:411 次
发布时间:2019-03-06

本文共 1645 字,大约阅读时间需要 5 分钟。

'; $xml .= "<{$root}>"; $xml .= array_to_xml($data); $xml .= "
"; return $xml; } function xml_decode($xml, $root = 'so') { $search = '/<(' . $root . ')>(.*)<\/\s*?\\1\s*?>/s'; $array = array(); if(preg_match($search, $xml, $matches)){ $array = xml_to_array($matches[2]); } return $array; } function array_to_xml($array) { if(is_object($array)){ $array = get_object_vars($array); } $xml = ''; foreach($array as $key => $value){ $_tag = $key; $_id = null; if(is_numeric($key)){ $_tag = 'item'; $_id = ' id="' . $key . '"'; } $xml .= "<{$_tag}{$_id}>"; $xml .= (is_array($value) || is_object($value)) ? array_to_xml($value) : htmlentities($value); $xml .= "
"; } return $xml; } function xml_to_array($xml) { $search = '/<(\w+)\s*?(?:[^\/>]*)\s*(?:\/>|>(.*?)<\/\s*?\\1\s*?>)/s'; $array = array (); if(preg_match_all($search, $xml, $matches)){ foreach ($matches[1] as $i => $key) { $value = $matches[2][$i]; if(preg_match_all($search, $value, $_matches)){ $array[$key] = xml_to_array($value); }else{ if('ITEM' == strtoupper($key)){ $array[] = html_entity_decode($value); }else{ $array[$key] = html_entity_decode($value); } } } } return $array; }

 

转载地址:http://iblkz.baihongyu.com/

你可能感兴趣的文章
netstat kill
查看>>
netstat命令用法详解
查看>>
Netstat端口占用情况
查看>>
Netty 4的内存管理:sun.misc.Unsafe
查看>>
Netty channelRegistered\ChannelActive---源码分析
查看>>
Netty NIO transport && OIO transport
查看>>
netty php,netty
查看>>
Netty WebSocket客户端
查看>>
netty 主要组件+黏包半包+rpc框架+源码透析
查看>>
Vue过渡 & 动画---vue工作笔记0014
查看>>
Netty 异步任务调度与异步线程池
查看>>
Netty 的 Handler 链调用机制
查看>>
Netty 编解码器和 Handler 调用机制
查看>>
Netty 编解码器详解
查看>>
Netty 解决TCP粘包/半包使用
查看>>
Netty 调用,效率这么低还用啥?
查看>>
Netty 高性能架构设计
查看>>
Netty+Protostuff实现单机压测秒级接收35万个对象实践经验分享
查看>>
Netty+SpringBoot+FastDFS+Html5实现聊天App详解(一)
查看>>
netty--helloword程序
查看>>