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);
}
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();
}
}
分享到:
相关推荐
php把数组转换成XML格式
用PHP将二维数组拼接成xml内容形式,形成的标签可以根据自己的需要进行修改
一个将数组转换为xml简单的PHP类
主要介绍了php实现将数组转换为XML的方法,实例分析了php操作数组及XML格式文件的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
将数组转换为xml此程序包提供了一个非常简单的类,可将数组转换为xml字符串。 安装您可以通过composer安装此软件包。 作曲家需要spatie / array-to-xml美国将数组转换为xml此包提供了一个非常简单的类,可以将数组...
主是描述了在php下array与Xml之间的转换总结,文档内有部分代码讲解!
xml 数据格式转换成php数组,php数组转成xml格式数据
php将对象转成数组,php将xml转成数组 一个自定义函数搞定纯原创。
主要介绍了php实现的数组转xml,结合具体案例形式分析了PHP数组与xml转换的实现方法与相关操作注意事项,需要的朋友可以参考下
下面是网上的 复制代码 代码如下: 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 ...
PHP查询xml,以数组返回PHP查询xml,以数组返回PHP查询xml,以数组返回PHP查询xml,以数组返回PHP查询xml,以数组返回PHP查询xml,以数组返回
xmlparse.php为封装好的xml与php数组相互转换的函数: XML->PHP: parse_xml_config(); PHP->XML: dump_xml_config(); #将转换好的XML放入文件: file_put_contents_safe().
把XML数据转换成php数组形式。
php中将xml转换成数组的方法,大部分情况下通用。
源码下载:PHP数组与XML之间的转换 PHP将数组转换成XML PHP可以将数组转换成xml格式,简单的办法是遍历数组,然后将数组的key/value转换成xml节点,再直接echo输出了,如: function arrayToXml($arr){ $xml = ...
主要介绍了php中Array2xml类实现数组转化成XML的方法,实例分析了数组转化成XML实现类Array2xml,是非常实用的数组转化技巧,需要的朋友可以参考下
APSArrayToXML.class.php 将数组转换为XMLString或XML对象