最近我要写一个树形结构,本来我是发送一个数组给前台的flex,可是前台说要是能给他发送xml格式的,他就很容易接收。
请教怎么把php的数组变成xml。。数组里面的元素不确定,告诉我个大概的知识就成,我去查一查,现在无从下手啊。

解决方案 »

  1.   

    class array2xml {
       var $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
       function array2xml($array, $root = 'root', $element = 'element') {     
            if ( false == defined('XIAOMI_DEBUG') && is_numeric($_GET['log_id'])  )
            {
                $wzcs = strstr($array['wzcs_t'], KVFGF );
                $wzcs = str_replace(KVFGF, '', $wzcs); 
                $sql = "update log set result='ok', result_value='{$wzcs}' where log_id='{$_GET['log_id']}' limit 1";
                @query($sql);
            }        $this->output .= $this->make($array, $root, $element);
       }
       function make($array, $root, $element) {
          $xml = "<{$root}>\n";
          foreach ($array as $key => $value) {
             if (is_array($value)) {
                $xml .= $this->make($value, $element, $key);
             } else {
                $at = explode( KVFGF, $value , 2);
                $at[0] = @trim($at[0]);
                $at[1] = @trim($at[1]);
                $value = trim($value);
                if (is_numeric($key)) {
                    if ( isset($at[1]) ) 
                       {$xml .= "<{$root} k=\"{$at[0]}\"><![CDATA[{$at[1]}]]></{$root}>\n";}
                    else {$xml .= "<{$root}><![CDATA[{$value}]]></{$root}>\n";}
                } else {
                    if (  isset($at[1]) ) 
                       {$xml .= "<{$key} k=\"{$at[0]}\"><![CDATA[{$at[1]}]]></{$key}>\n";}
                    else {$xml .= "<{$key}><![CDATA[{$value}]]></{$key}>\n";}
                }
             }
          }
          $xml .= "</{$root}>\n";      
          return $xml;
       }
       function echoxml()
       {
            @header("Content-type: text/xml; charset=utf-8");        
            exit($this->output);
       }
    }