博客
关于我
array与xml转换实现(转)
阅读量:408 次
发布时间: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/

你可能感兴趣的文章
【设计模式】单例模式
查看>>
远程触发Jenkins的Pipeline任务的并发问题处理
查看>>
entity framework core在独立类库下执行迁移操作
查看>>
Asp.Net Core 2.1+的视图缓存(响应缓存)
查看>>
【wp】HWS计划2021硬件安全冬令营线上选拔赛
查看>>
Ef+T4模板实现代码快速生成器
查看>>
JQuery选择器
查看>>
Java面试题:Servlet是线程安全的吗?
查看>>
Java集合总结系列2:Collection接口
查看>>
Linux学习总结(九)—— CentOS常用软件安装:中文输入法、Chrome
查看>>
比技术还重要的事
查看>>
linux线程调度策略
查看>>
软中断和实时性
查看>>
Linux探测工具BCC(可观测性)
查看>>
HDU5589:Tree(莫队+01字典树)
查看>>
Python开发之序列化与反序列化:pickle、json模块使用详解
查看>>
采坑 - 字符串的 "" 与 pd.isnull()
查看>>
无序列表 - 链表
查看>>
Matplotlib绘制漫威英雄战力图,带你飞起来!
查看>>
机器学习是什么
查看>>