php数组转换成xml文件php类
发布:smiling 来源: PHP粉丝网 添加日期:2014-08-20 14:50:07 浏览: 评论:0
我们经常会用到缓存数据就是把数组保存成xml文档方便处理,下面我们提供一个数组转换成xml文档的类,有需要的朋友可以参考一下.
php数组转换成xml文件php类代码如下:
- <?php
- $elementLevel = 0 ;
- function array_Xml($array, $keys = '')
- {
- global $elementLevel;
- if(!is_array($array))
- {
- if($keys == ''){
- return $array;
- }else{
- return "n<$keys>" . $array . "</$keys>";
- } //开源代码phpfensi.com
- }else{
- foreach ($array as $key => $value)
- {
- $haveTag = true;
- if (is_numeric($key))
- {
- $key = $keys;
- $haveTag = false;
- }
- /**
- * The first element
- */
- if($elementLevel == 0 )
- {
- $startElement = "<$key>";
- $endElement = "</$key>";
- }
- $text .= $startElement."n";
- /**
- * Other elements
- */
- if(!$haveTag)
- {
- $elementLevel++;
- $text .= "<$key>" . array_Xml($value, $key). "</$key>n";
- }else{
- $elementLevel++;
- $text .= array_Xml($value, $key);
- }
- $text .= $endElement."n";
- }
- }
- return $text;
- }
- 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();
- }
- }
- function arrtoxml($arr,$dom=0,$item=0){
- if (!$dom){
- $dom = new DOMDocument("1.0");
- }
- if(!$item){
- $item = $dom->createElement("root");
- $dom->appendChild($item);
- }
- foreach ($arr as $key=>$val){
- $itemx = $dom->createElement(is_string($key)?$key:"item");
- $item->appendChild($itemx);
- if (!is_array($val)){
- $text = $dom->createTextNode($val);
- $itemx->appendChild($text);
- }else {
- arrtoxml($val,$dom,$itemx);
- }
- }
- return $dom->saveXML();
- }
- ?>
Tags: php数组转换成xml文件 php类
- 上一篇:PHP解析JSON与XML程序
- 下一篇:php读取树型xml文件实现类
相关文章
- ·PHP类中的静态方法使用实例(2014-02-18)
- ·php类和对象之protected与const属性(2014-03-06)
- ·PHP类型转换的面试题与答案解析(2015-12-24)
- ·浅谈PHP中其他类型转化为Bool类型(2019-11-11)
- ·PHP类型约束的详细介绍(附代码)(2020-02-11)
- ·PHP类的反射用法实例(2021-04-22)
- ·PHP中的类型约束介绍(2021-05-26)
- ·PHP中的类型提示(type hinting)功能介绍(2021-06-06)
- ·php类常量用法实例分析(2021-06-10)
- ·深入理解PHP变量的值类型和引用类型(2021-06-20)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)