读取xml文件,php 读取html文件,如何读取xml文件
$obj = new JZH_xml("
$obj->setChar('gb2312');
$obj->getElementByTagname('string',0)->value;
$obj->getstring(0)->value;
...
/*
轻便的xml文档操作
setChar 编码转换
parse 解析xml文档
parse_file 从文件解析xml文档
getElementByTagname 获得tag对象
get
*/
class JZH_xml{
var $index;
var $vals;
var $in='utf-8';
var $out='utf-8';
var $xmlobj;
function JZH_xml($data=false,$in=false,$out=false){
$this->xmlobj = xml_parser_create();
if($data){
xml_parse_into_struct($this->xmlobj, $data, $this->vals, $this->index);
xml_parser_free($this->xmlobj);
}
}
function setChar($out=false,$in=false){
if($in)$this->in=$in;
if($out)$this->out=$out;
array_walk_recursive($this->vals,array($this,'iconv'));
}
function iconv(&$value, $key){
$value=iconv($this->in,$this->out,$value);
}
function parse($data){
xml_parse_into_struct($this->xmlobj, $data, $this->vals, $this->index);
xml_parser_free($this->xmlobj);
}
function parse_file($file){
$data = file_get_contents($file);
$this->parse($data);
}
function getElementByTagname($tag,$index){
$tag = strtoupper($tag);
if(!isset($this->index[$tag][$index]))return false;
$index = $this->index[$tag][$index];
return (object)$this->vals[$index];
}
function __call($name,$arguments){
if(($tag=str_replace('GET','',strtoupper($name)))==strtoupper($name))return false;
return $this->getElementByTagname($tag,$arguments[0]);
}
}