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;
    }
    

      

  • 相关阅读:
    排列组合
    分治——最大连续数组和
    分治——最近点对
    Java数据类型
    4源代码的下载和编译
    3Git使用入门
    2.3搭建Android应用程序开发环境
    2.2安装JDK
    2.1Android底层开发需要哪些工具
    1.8小结
  • 原文地址:https://www.cnblogs.com/wntd/p/9628698.html
Copyright © 2011-2022 走看看