使用simplexml_load_file()加载gzip压缩的XML文件

时间:2011-08-07 15:19:02

标签: php gzip simplexml decode

我正在尝试使用simplexml_load_file()PHP函数加载一个gzip压缩文件,但我不知道如何解码它以便我可以使用它中的数据。

3 个答案:

答案 0 :(得分:11)

PHP内置support for zlib compression,只需使用compress.zlib://的gzip数据为文件路径添加前缀,即可完成:

$xml = simplexml_load_file("compress.zlib://test.xml");

像魅力一样。

答案 1 :(得分:4)

/*EDIT  print_r(gzdecoder(simplexml_load_file('test.xml')));
 (Not sure without testing that the internals of what loads the xml 
  in *_load_file would see the gzipped content as invalid)
*/

   $xml=simplexml_load_string(gzdecoder(file_get_contents('test.xml')));
   print_r( $xml);

function gzdecoder($d){
    $f=ord(substr($d,3,1));
    $h=10;$e=0;
    if($f&4){
        $e=unpack('v',substr($d,10,2));
        $e=$e[1];$h+=2+$e;
    }
    if($f&8){
        $h=strpos($d,chr(0),$h)+1;
    }
    if($f&16){
        $h=strpos($d,chr(0),$h)+1;
    }
    if($f&2){
        $h+=2;
    }
    $u = gzinflate(substr($d,$h));
    if($u===FALSE){
        $u=$d;
    }
    return $u;
}

答案 2 :(得分:3)

使用file_get_contents()将其读取为字符串 使用gzuncompress()解压缩 并使用simplexml_load_string()将字符串加载为XML。

相关问题