博客
关于我
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/

你可能感兴趣的文章
Mysql:SQL性能分析
查看>>
mysql:SQL按时间查询方法总结
查看>>
MySQL:什么样的字段适合加索引?什么样的字段不适合加索引
查看>>
MySQL:判断逗号分隔的字符串中是否包含某个字符串
查看>>
MySQL:某个ip连接mysql失败次数过多,导致ip锁定
查看>>
Mysql:避免重复的插入数据方法汇总
查看>>
m_Orchestrate learning system---二十二、html代码如何变的容易
查看>>
n = 3 , while n , continue
查看>>
n 叉树后序遍历转换为链表问题的深入探讨
查看>>
N-Gram的基本原理
查看>>
nacos config
查看>>
Nacos 与 Eureka、Zookeeper 和 Consul 等其他注册中心的区别
查看>>
Nacos2.X 配置中心源码分析:客户端如何拉取配置、服务端配置发布客户端监听机制
查看>>
NacosClient客户端搭建,微服务注册进nacos
查看>>
Nacos原理
查看>>
Nacos发布0.5.0版本,轻松玩转动态 DNS 服务
查看>>
Nacos启动异常
查看>>
Nacos和Zookeeper对比
查看>>
Nacos在双击startup.cmd启动时提示:Unable to start embedded Tomcat
查看>>
Nacos如何实现Raft算法与Raft协议原理详解
查看>>