zoukankan      html  css  js  c++  java
  • array与xml转换实现(转)

        <?php
        function xml_encode($data, $charset = 'utf-8', $root = 'so') {
            $xml = '<?xml version="1.0" encoding="' . $charset .'"?>';
            $xml .= "<{$root}>";
            $xml .= array_to_xml($data);   
            $xml .= "</{$root}>";
            return $xml;
        }
    
        function xml_decode($xml, $root = 'so') {
            $search = '/<(' . $root . ')>(.*)</s*?\1s*?>/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 .= "</{$_tag}>";
            }
            return $xml;
        }
    
        function xml_to_array($xml) {
            $search = '/<(w+)s*?(?:[^/>]*)s*(?:/>|>(.*?)</s*?\1s*?>)/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;
        }
    
  • 相关阅读:
    百度和谷歌,你选择谁?
    数据库的另一种设计方法
    超级IO操作类
    WEB工具类,很强很大
    JS在AJAX中获取鼠标坐标
    弃掉HTML标记的小巧代码
    XML工具操作类,很强大
    FTP 下载功能代码
    db4o开门之篇
    ASP.NET程序中常用代码汇总(转载)
  • 原文地址:https://www.cnblogs.com/chenqionghe/p/4379620.html
Copyright © 2011-2022 走看看