1. 程式人生 > >XML轉JSON---微信支付返回XML轉換程式碼

XML轉JSON---微信支付返回XML轉換程式碼

// ********XML轉JSON方法

function xml_to_json($source) { 
if(is_file($source)){ //傳的是檔案,還是xml的string的判斷 
$xml_array=simplexml_load_file($source); 
}else{ 
$source=$this->uncdata($source); 

$xml_array=simplexml_load_string($source);	

} 
$json = json_encode($xml_array); //php5,以及以上,如果是更早版本,請檢視JSON.php 
return $this->decodeUnicode($json); 
} 



function decodeUnicode($str) 
{ 
    return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', 
        create_function( 
            '$matches', 
            'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");' 
        ), 
        $str); 
}
function uncdata($xml) 
    { 
        // States: 
        // 
        //     'out' 
        //     '<' 
        //     '<!' 
        //     '<![' 
        //     '<![C' 
        //     '<![CD' 
        //     '<![CDAT' 
        //     '<![CDATA' 
        //     'in' 
        //     ']' 
        //     ']]' 
        // 
        // (Yes, the states a represented by strings.) 
        // 

        $state = 'out'; 

        $a = str_split($xml); 

        $new_xml = ''; 

        foreach ($a AS $k => $v) { 

            // Deal with "state". 
            switch ( $state ) { 
                case 'out': 
                    if ( '<' == $v ) { 
                        $state = $v; 
                    } else { 
                        $new_xml .= $v; 
                    } 
                break; 

                case '<': 
                    if ( '!' == $v  ) { 
                        $state = $state . $v; 
                    } else { 
                        $new_xml .= $state . $v; 
                        $state = 'out'; 
                    } 
                break; 

                 case '<!': 
                    if ( '[' == $v  ) { 
                        $state = $state . $v; 
                    } else { 
                        $new_xml .= $state . $v; 
                        $state = 'out'; 
                    } 
                break; 

                case '<![': 
                    if ( 'C' == $v  ) { 
                        $state = $state . $v; 
                    } else { 
                        $new_xml .= $state . $v; 
                        $state = 'out'; 
                    } 
                break; 

                case '<![C': 
                    if ( 'D' == $v  ) { 
                        $state = $state . $v; 
                    } else { 
                        $new_xml .= $state . $v; 
                        $state = 'out'; 
                    } 
                break; 

                case '<![CD': 
                    if ( 'A' == $v  ) { 
                        $state = $state . $v; 
                    } else { 
                        $new_xml .= $state . $v; 
                        $state = 'out'; 
                    } 
                break; 

                case '<![CDA': 
                    if ( 'T' == $v  ) { 
                        $state = $state . $v; 
                    } else { 
                        $new_xml .= $state . $v; 
                        $state = 'out'; 
                    } 
                break; 

                case '<![CDAT': 
                    if ( 'A' == $v  ) { 
                        $state = $state . $v; 
                    } else { 
                        $new_xml .= $state . $v; 
                        $state = 'out'; 
                    } 
                break; 

                case '<![CDATA': 
                    if ( '[' == $v  ) { 


                        $cdata = ''; 
                        $state = 'in'; 
                    } else { 
                        $new_xml .= $state . $v; 
                        $state = 'out'; 
                    } 
                break; 

                case 'in': 
                    if ( ']' == $v ) { 
                        $state = $v; 
                    } else { 
                        $cdata .= $v; 
                    } 
                break; 

                case ']': 
                    if (  ']' == $v  ) { 
                        $state = $state . $v; 
                    } else { 
                        $cdata .= $state . $v; 
                        $state = 'in'; 
                    } 
                break; 

                case ']]': 
                    if (  '>' == $v  ) { 
                        $new_xml .= str_replace('>','&gt;', 
                                    str_replace('>','&lt;', 
                                    str_replace('"','&quot;', 
                                    str_replace('&','&amp;', 
                                    $cdata)))); 
                        $state = 'out'; 
                    } else { 
                        $cdata .= $state . $v; 
                        $state = 'in'; 
                    } 
                break; 
            } // switch 

        } 

        // 
        // Return. 
        // 
            return $new_xml; 

    } 
 
//*********/**************////