用sax方式的时候,要自己构建3个函数, 而且要直接用这三的函数来返回数据, 要求较强的逻辑. 在处理不同结构的xml的时候, 还要重新进行构造这三个函数,麻烦!
用dom方式,倒是好些,但是他把每个节点都看作是一个node, 操作起来要写好多的代码, 麻烦!
网上有好多的开源的xml解析的类库, 以前看过几个, 但是心里总是觉得不踏实, 感觉总是跟在别人的屁股后面.
这几天在搞java, 挺累的, 所以决定换换脑袋, 写点php代码, 为了防止以后xml解析过程再令我犯难, 就花了一天的时间写了下面一个xml解析的类, 于是就有了下面的东西,
实现方式是通过包装sax方式的解析结果来实现的. 总的来说,对于我个人来说挺实用的,性能也还可以,基本上可以完成大多数的处理要求。
功能:
1\ 对基本的xml文件的节点进行 查询 / 添加 / 修改 / 删除 工作.
2\ 导出xml文件的所有数据到一个数组里面.
3\ 整个设计采用了oo方式,在操作结果集的时候, 使用方法类似于dom
缺点:
1\ 每个节点最好都带有一个id(看后面的例子), 每个“节点名字”=“节点的标签_节点的id”,如果这个id值没有设置,程序将自动给他产生一个id,这个id就是这个节点在他的上级节点中的位置编号,从0开始。
2\ 查询某个节点的时候可以通过用“|”符号连接“节点名字”来进行。这些“节点名字”都是按顺序写好的上级节点的名字。
使用说明:
运行下面的例子,在执行结果页面上可以看到函数的使用说明
代码是通过php5来实现的,在php4中无法正常运行。
由于刚刚写完,所以没有整理文档,下面的例子演示的只是一部分的功能,代码不是很难,要是想知道更多的功能,可以研究研究源代码。
目录结构:
test.php test.xml xml / simpledocumentbase.php xml / simpledocumentnode.php xml / simpledocumentroot.php xml / simpledocumentparser.php
文件:test.xml
华联
北京长安街-9999号
连锁超市
food11
12.90
food12
22.10
好东西推荐
tel21
1290
coat31
112
coat32
45
hot41
99
文件:test.php parse(test.xml);
$dom = $test->getsimpledocument();
echo
;echo
;
echo 下面是通过函数getsavedata()返回的整个xml数据的数组;
echo ;
print_r($dom->getsavedata());echo
;
echo 下面是通过setvalue()函数,给给根节点添加信息,添加后显示出结果xml文件的内容;
echo ;
$dom->setvalue(telphone, 123456789);
echo htmlspecialchars($dom->getsavexml());echo
;
echo 下面是通过getnode()函数,返回某一个分类下的所有商品的信息;
echo ;
$obj = $dom->getnode(cat_food);
$nodelist = $obj->getnode();
foreach($nodelist as $node){
$data = $node->getvalue();
echo 商品名:.$data[name].
;
print_r($data);
print_r($node->getattribute());
}echo
;
echo 下面是通过findnodebypath()函数,返回某一商品的信息;
echo ;
$obj = $dom->findnodebypath(cat_food|goods_food11);
if(!is_object($obj)){
echo 该商品不存在;
}else{
$data = $obj->getvalue();
echo 商品名:.$data[name].
;
print_r($data);
print_r($obj->getattribute());
}echo
;
echo 下面是通过setvalue()函数,给商品\food11\添加属性, 然后显示添加后的结果;
echo ;
$obj = $dom->findnodebypath(cat_food|goods_food11);
$obj->setvalue(leaveword, array(value=>这个商品不错, attrs=>array(author=>hahawen, date=>date('y-m-d'))));
echo htmlspecialchars($dom->getsavexml());echo
;
echo 下面是通过removevalue()/removeattribute()函数,给商品\food11\改变和删除属性, 然后显示操作后的结果;
echo ;
$obj = $dom->findnodebypath(cat_food|goods_food12);
$obj->setvalue(name, new food12);
$obj->removevalue(desc);
echo htmlspecialchars($dom->getsavexml());echo
;
echo 下面是通过createnode()函数,添加商品, 然后显示添加后的结果;
echo ;
$obj = $dom->findnodebypath(cat_food);
$newobj = $obj->createnode(goods, array(id=>food13));
$newobj->setvalue(name, food13);
$newobj->setvalue(price, 100);
echo htmlspecialchars($dom->getsavexml());echo
;
echo 下面是通过removenode()函数,删除商品, 然后显示删除后的结果;
echo ;
$obj = $dom->findnodebypath(cat_food);
$obj->removenode(goods_food12);
echo htmlspecialchars($dom->getsavexml());
?>
文件:simpledocumentparser.php domrootobject; } public function parse($file) { $xmlparser = xml_parser_create(); xml_parser_set_option($xmlparser,xml_option_case_folding, 0); xml_parser_set_option($xmlparser,xml_option_skip_white, 1); xml_parser_set_option($xmlparser, xml_option_target_encoding, 'utf-8'); xml_set_object($xmlparser, $this); xml_set_element_handler($xmlparser, startelement, endelement); xml_set_character_data_handler($xmlparser, characterdata); if (!xml_parse($xmlparser, file_get_contents($file))) die(sprintf(xml error: %s at line %d, xml_error_string(xml_get_error_code($xmlparser)), xml_get_current_line_number($xmlparser))); xml_parser_free($xmlparser); } private function startelement($parser, $name, $attrs) { $this->currentname = $name; $this->currentattribute = $attrs; if($this->currentno == null) { $this->domrootobject = new simpledocumentroot($name); $this->currentno = $this->domrootobject; } else { $this->currentno = $this->currentno->createnode($name, $attrs); } } private function endelement($parser, $name) { if($this->currentname==$name) { $seq = $this->currentno->getseq();
$this->currentno = $this->currentno->getpnodeobject();
$tag = $this->currentno->getnodename($seq);
if($this->currentattribute!=null && sizeof($this->currentattribute)>0) $this->currentno->setvalue($name, array('value'=>$this->currentvalue, 'attrs'=>$this->currentattribute)); else $this->currentno->setvalue($name, $this->currentvalue); $this->currentno->removenode($tag); } else { $this->currentno = (is_a($this->currentno, 'simpledocumentroot'))? null: $this->currentno->getpnodeobject(); } } private function characterdata($parser, $data) { $this->currentvalue = iconv('utf-8', 'gb2312', $data); } function __destruct() { unset($this->domrootobject); }}?>
文件:simpledocumentbase.php nodetag = $nodetag; } public function getnodetag() { return $this->nodetag; } public function setvalues($values){ $this->values = $values; } public function setvalue($name, $value) { $this->values[$name] = $value; } public function getvalue($name=null) { return $name==null? $this->values: $this->values[$name]; } public function removevalue($name) { unset($this->values[$name]); } public function setattributes($attributes){ $this->attributes = $attributes; } public function setattribute($name, $value) { $this->attributes[$name] = $value; } public function getattribute($name=null) { return $name==null? $this->attributes: $this->attributes[$name]; } public function removeattribute($name) { unset($this->attributes[$name]); } public function getnodessize() { return sizeof($this->nodes); } protected function setnode($name, $nodeid) { $this->nodes[$name] = $nodeid; } public abstract function createnode($name, $attributes); public abstract function removenode($name); public abstract function getnode($name=null); protected function getnodeid($name=null) { return $name==null? $this->nodes: $this->nodes[$name]; }
public function getnodename($id)
{
$tmp = array_flip($this->nodes);
return $tmp[$id];
} protected function createnodebyname($rootnodeobj, $name, $attributes, $pid) { $tmpobject = $rootnodeobj->createnodeobject($pid, $name, $attributes); $key = isset($attributes[id])? $name.'_'.$attributes[id]: $name.'_'.$this->getnodessize(); $this->setnode($key, $tmpobject->getseq()); return $tmpobject; } protected function removenodebyname($rootnodeobj, $name) { $rootnodeobj->removenodebyid($this->getnodeid($name)); if(sizeof($this->nodes)==1) $this->nodes = array(); else unset($this->nodes[$name]); } protected function getnodebyname($rootnodeobj, $name=null) { if($name==null) { $tmplist = array(); $tmpids = $this->getnodeid(); foreach($tmpids as $key=>$id) $tmplist[$key] = $rootnodeobj->getnodebyid($id); return $tmplist; } else { $id = $this->getnodeid($name); if($id===null) { $tmpids = $this->getnodeid(); foreach($tmpids as $tkey=>$tid) { if(strpos($key, $name)==0) { $id = $tid; break; } } } return $rootnodeobj->getnodebyid($id); } } public function findnodebypath($path) { $pos = strpos($path, '|'); if($posgetnode($path); } else { $tmpobj = $this->getnode(substr($path, 0, $pos)); return is_object($tmpobj)? $tmpobj->findnodebypath(substr($path, $pos+1)): null; } } public function getsavedata() { $data = $this->values; if(sizeof($this->attributes)>0) $data[attrs] = $this->attributes; $nodelist = $this->getnode(); if($nodelist==null) return $data; foreach($nodelist as $key=>$node) { $data[$key] = $node->getsavedata(); } return $data; } public function getsavexml($level=0) { $prefixspace = str_pad(, $level, \t); $str = $prefixspacenodetag; foreach($this->attributes as $key=>$value) $str .= $key=\$value\; $str .= >\r\n; foreach($this->values as $key=>$value){ if(is_array($value)) { $str .= $prefixspace\t$attvalue) $str .= $attkey=\$attvalue\; $tmpstr = $value[value]; } else { $str .= $prefixspace\t\r\n: >$tmpstr$key>\r\n; } foreach($this->getnode() as $node) $str .= $node->getsavexml($level+1).\r\n; $str .= $prefixspace$this->nodetag>; return $str; } function __destruct() { unset($this->nodes, $this->attributes, $this->values); }}?>
文件:simpledocumentroot.php nodelists); $tmpobject = new simpledocumentnode($this, $pnodeid, $name, $seq); $tmpobject->setattributes($attributes); $this->nodelists[$seq] = $tmpobject; return $tmpobject; } public function removenodebyid($id) { if(sizeof($this->nodelists)==1) $this->nodelists = array(); else unset($this->nodelists[$id]); } public function getnodebyid($id) { return $this->nodelists[$id]; } public function createnode($name, $attributes) { return $this->createnodebyname($this, $name, $attributes, -1); } public function removenode($name) { return $this->removenodebyname($this, $name); } public function getnode($name=null) { return $this->getnodebyname($this, $name); } public function getsavexml() { $prefixspace = ; $str = $this->prefixstr.\r\n; return $str.parent::getsavexml(0); }}?>
文件:simpledocumentnode.php rootobject = $rootobject; $this->pnodeid = $pnodeid; $this->seq = $seq; } public function getpnodeobject() { return ($this->pnodeid==-1)? $this->rootobject: $this->rootobject->getnodebyid($this->pnodeid); } public function getseq(){ return $this->seq; } public function createnode($name, $attributes) { return $this->createnodebyname($this->rootobject, $name, $attributes, $this->getseq()); } public function removenode($name) { return $this->removenodebyname($this->rootobject, $name); } public function getnode($name=null) { return $this->getnodebyname($this->rootobject, $name); }}?>
下面是例子运行对结果: 下面是通过函数getsavedata()返回的整个xml数据的数组array( [name] => 华联 [address] => 北京长安街-9999号 [desc] => 连锁超市 [cat_food] => array ( [attrs] => array ( [id] => food ) [goods_food11] => array ( [name] => food11 [price] => 12.90 [attrs] => array ( [id] => food11 ) ) [goods_food12] => array ( [name] => food12 [price] => 22.10 [desc] => array ( [value] => 好东西推荐 [attrs] => array ( [creator] => hahawen ) ) [attrs] => array ( [id] => food12 ) ) ) [cat_1] => array ( [goods_tel21] => array ( [name] => tel21 [price] => 1290 [attrs] => array ( [id] => tel21 ) ) ) [cat_coat] => array ( [attrs] => array ( [id] => coat ) [goods_coat31] => array ( [name] => coat31 [price] => 112 [attrs] => array ( [id] => coat31 ) ) [goods_coat32] => array ( [name] => coat32 [price] => 45 [attrs] => array ( [id] => coat32 ) ) ) [special_hot] => array ( [attrs] => array ( [id] => hot ) [goods_0] => array ( [name] => hot41 [price] => 99 ) ))下面是通过setvalue()函数,给给根节点添加信息,添加后显示出结果xml文件的内容华联北京长安街-9999号 连锁超市123456789food1112.90food1222.10好东西推荐tel211290coat31112coat3245hot4199下面是通过getnode()函数,返回某一个分类下的所有商品的信息商品名:food11
array( [name] => food11 [price] => 12.90)array( [id] => food11)商品名:food12
array( [name] => food12 [price] => 22.10 [desc] => array ( [value] => 好东西推荐 [attrs] => array ( [creator] => hahawen ) ))array( [id] => food12)下面是通过findnodebypath()函数,返回某一商品的信息商品名:food11
array( [name] => food11 [price] => 12.90)array( [id] => food11)下面是通过setvalue()函数,给商品food11添加属性, 然后显示添加后的结果华联北京长安街-9999号 连锁超市123456789food1112.90这个商品不错food1222.10好东西推荐tel211290coat31112coat3245hot4199下面是通过removevalue()/removeattribute()函数,给商品food11改变和删除属性, 然后显示操作后的结果华联北京长安街-9999号 连锁超市123456789food1112.90这个商品不错new food1222.10tel211290coat31112coat3245hot4199下面是通过createnode()函数,添加商品, 然后显示添加后的结果华联北京长安街-9999号 连锁超市123456789food1112.90这个商品不错new food1222.10food13100tel211290coat31112coat3245hot4199下面是通过removenode()函数,删除商品, 然后显示删除后的结果华联北京长安街-9999号 连锁超市123456789food1112.90这个商品不错food13100tel211290coat31112coat3245hot4199
