php 数组转xml的例子
发布:smiling 来源: PHP粉丝网 添加日期:2016-07-15 15:57:25 浏览: 评论:0
数组转xml用到不多用数组转json比较多了,但今天有一个功能就是必须要把数组转换成xml了,下面小编人网上找到了一段关于php 数组转xml的代码测试了都可以使用,下面整理分享给各位。
例子1:
下面这个可以支持多维数组,测试代码:test.php
- <?php
- include './ArrayToXML.php';
- header('Content-Type: text/xml');
- $data=array("name"=>"zhangsan","sex"=>"0","address"=>array("sheng"=>"chongqing","shi"=>"nanchuan","zhen"=>"daguan"));
- echo ArrayToXML::toXml($data);
- ?>
处理代码:ArrayToXML.php
- <?php
- class ArrayToXML
- {
- /**
- * The main function for converting to an XML document.
- * Pass in a multi dimensional array and this recrusively loops through and builds up an XML document.
- *
- * @param array $data
- * @param string $rootNodeName - what you want the root node to be - defaultsto data.
- * @param SimpleXMLElement $xml - should only be used recursively
- * @return string XML
- */
- public static function toXml($data, $rootNodeName = 'data', $xml=null)
- {
- // turn off compatibility mode as simple xml throws a wobbly if you don't.
- if (ini_get('zend.ze1_compatibility_mode') == 1)
- {
- ini_set ('zend.ze1_compatibility_mode', 0);
- }
- if ($xml == null)
- {
- $xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$rootNodeName />");
- }
- // loop through the data passed in.
- foreach($data as $key => $value)
- {
- // no numeric keys in our xml please!
- if (is_numeric($key))
- {
- // make string key...
- $key = "unknownNode_". (string) $key;
- }
- // replace anything not alpha numeric
- $key = preg_replace('/[^a-z]/i', '', $key);
- // if there is another array found recrusively call this function
- if (is_array($value))
- {
- $node = $xml->addChild($key);
- // recrusive call.
- ArrayToXML::toXml($value, $rootNodeName, $node);
- } //phpfensi.com
- else
- {
- // add single node.
- $value = htmlentities($value);
- $xml->addChild($key,$value);
- }
- }
- // pass back as string. or simple xml object if you want!
- return $xml->asXML();
- }
- }
ArrayToXML.php来源于网络,本人不做任何解释.
例子2:
- // Xml 转 数组, 包括根键,忽略空元素和属性,尚有重大错误
- function xml_to_array( $xml )
- {
- $reg = "/<(\\w+)[^>]*?>([\\x00-\\xFF]*?)<\\/\\1>/";
- if(preg_match_all($reg, $xml, $matches))
- {
- $count = count($matches[0]);
- $arr = array();
- for($i = 0; $i < $count; $i++)
- {
- $key= $matches[1][$i];
- $val = xml_to_array( $matches[2][$i] ); // 递归
- if(array_key_exists($key, $arr))
- {
- if(is_array($arr[$key]))
- {
- if(!array_key_exists(0,$arr[$key]))
- {
- $arr[$key] = array($arr[$key]);
- }
- }else{
- $arr[$key] = array($arr[$key]);
- }
- $arr[$key][] = $val;
- }else{
- $arr[$key] = $val;
- }
- }
- return $arr;
- }else{
- return $xml;
- }
- }
- // Xml 转 数组, 不包括根键
- function xmltoarray( $xml )
- {
- $arr = xml_to_array($xml);
- $key = array_keys($arr);
- return $arr[$key[0]];
- }
- // 类似 XPATH 的数组选择器
- function xml_array_select( $arr, $arrpath )
- {
- $arrpath = trim( $arrpath, '/' );
- if(!$arrpath) return $arr;
- $self = 'xml_array_select';
- $pos = strpos( $arrpath, '/' );
- $pos = $pos ? $pos : strlen($arrpath);
- $curpath = substr($arrpath, 0, $pos);
- $next = substr($arrpath, $pos);
- if(preg_match("/\\[(\\d+)\\]$/",$curpath,$predicate))
- {
- $curpath = substr($curpath, 0, strpos($curpath,"[{$predicate[1]}]"));
- $result = $arr[$curpath][$predicate[1]];
- }else $result = $arr[$curpath];
- if( is_array($arr) && !array_key_exists($curpath, $arr) )
- {
- die( 'key is not exists:' . $curpath );
- }
- return $self($result, $next);
- }
- // 如果输入的数组是全数字键,则将元素值依次传输到 $callback, 否则将自身传输给$callback
- function xml_array_each( $arr, $callback )
- {
- if(func_num_args()<2) die('parameters error');
- if(!is_array($arr)) die('parameter 1 shuld be an array!');
- if(!is_callable($callback)) die('parameter 2 shuld be an function!');
- $keys = array_keys($arr);
- $isok = true;
- foreach( $keys as $key ) {if(!is_int($key)) {$isok = false; break;}}
- if($isok)
- foreach( $arr as $val ) $result[] = $callback($val);
- else
- $result[] = $callback( $arr );
- return $result;
- }
- /**
- * 最简单的XML转数组
- * @param string $xmlstring XML字符串
- * @return array XML数组
- */
- function simplest_xml_to_array($xmlstring) {
- return json_decode(json_encode((array) simplexml_load_string($xmlstring)), true);
- }
Tags: 数组 例子
- 上一篇:php二维数组多元素排序例子
- 下一篇:php多维数组的排序方式
相关文章
- ·PHP中数组定义的几种方法(2013-11-13)
- ·php学习笔记 [预定义数组(超全局数组)](2013-11-13)
- ·php session 预定义数组(2013-11-13)
- ·PHP 数组教程 定义数组(2013-11-13)
- ·我要如何在HTML中建立<form>数组?(2013-11-27)
- ·php 读取多维数组方法(2013-11-28)
- ·删除数组与二维数组值的php代码(2013-11-29)
- ·php 数组之删除空数组程序(2013-11-30)
- ·php删除数组中的空值或指定值操作(2013-11-30)
- ·PHP数组传递给JavaScript以及json_encode的gbk中文乱码(2013-12-02)
- ·php对数组元素去重复值(2013-12-03)
- ·php数组提示Notice: Undefined offset解决办法(2013-12-04)
- ·解决php array数组生成xml文件汉字编码问题(2013-12-07)
- ·php中向数组中插入一元素程序代码(2014-01-03)
- ·php删除数组元素几种方法(2014-01-03)
- ·php中count 多维数组长度统计实现方法(2014-01-03)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)