用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
<?xml version="1.0" encoding="gb2312"?>
<shop>
<name>华联</name>
<address>北京长安街-9999号</address>
<desc>连锁超市</desc>
<cat id="food">
<goods id="food11">
<name>food11</name>
<price>12.90</price>
</goods>
<goods id="food12">
<name>food12</name>
<price>22.10</price>
<desc creator="hahawen">好东西推荐</desc>
</goods>
</cat>
<cat>
<goods id="tel21">
<name>tel21</name>
<price>1290</price>
</goods>
</cat>
<cat id="coat">
<goods id="coat31">
<name>coat31</name>
<price>112</price>
</goods>
<goods id="coat32">
<name>coat32</name>
<price>45</price>
</goods>
</cat>
<special id="hot">
<goods>
<name>hot41</name>
<price>99</price>
</goods>
</special>
</shop>
<?php
require_once "xml/simpledocumentparser.php";
require_once "xml/simpledocumentbase.php";
require_once "xml/simpledocumentroot.php";
require_once "xml/simpledocumentnode.php";
$test = new simpledocumentparser();
$test->parse("test.xml");
$dom = $test->getsimpledocument();
echo "<pre>";
echo "<hr><font color=red>";
echo "下面是通过函数getsavedata()返回的整个xml数据的数组";
echo "</font><hr>";
print_r($dom->getsavedata());
echo "<hr><font color=red>";
echo "下面是通过setvalue()函数,给给根节点添加信息,添加后显示出结果xml文件的内容";
echo "</font><hr>";
$dom->setvalue("telphone", "123456789");
echo htmlspecialchars($dom->getsavexml());
echo "<hr><font color=red>";
echo "下面是通过getnode()函数,返回某一个分类下的所有商品的信息";
echo "</font><hr>";
$obj = $dom->getnode("cat_food");
$nodelist = $obj->getnode();
foreach($nodelist as $node){
$data = $node->getvalue();
echo "<font color=red>商品名:".$data[name]."</font><br>";
print_r($data);
print_r($node->getattribute());
}
echo "<hr><font color=red>";
echo "下面是通过findnodebypath()函数,返回某一商品的信息";
echo "</font><hr>";
$obj = $dom->findnodebypath("cat_food|goods_food11");
if(!is_object($obj)){
echo "该商品不存在";
}else{
$data = $obj->getvalue();
echo "<font color=red>商品名:".$data[name]."</font><br>";
print_r($data);
print_r($obj->getattribute());
}
echo "<hr><font color=red>";
echo "下面是通过setvalue()函数,给商品/"food11/"添加属性, 然后显示添加后的结果";
echo "</font><hr>";
$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 "<hr><font color=red>";
echo "下面是通过removevalue()/removeattribute()函数,给商品/"food11/"改变和删除属性, 然后显示操作后的结果";
echo "</font><hr>";
$obj = $dom->findnodebypath("cat_food|goods_food12");
$obj->setvalue("name", "new food12");
$obj->removevalue("desc");
echo htmlspecialchars($dom->getsavexml());
echo "<hr><font color=red>";
echo "下面是通过createnode()函数,添加商品, 然后显示添加后的结果";
echo "</font><hr>";
$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 "<hr><font color=red>";
echo "下面是通过removenode()函数,删除商品, 然后显示删除后的结果";
echo "</font><hr>";
$obj = $dom->findnodebypath("cat_food");
$obj->removenode("goods_food12");
echo htmlspecialchars($dom->getsavexml());
<?php/** *========================================================= * * @author hahawen(大龄青年)* @since 2004-12-04 * @copyright copyright (c) 2004, nxcoder group * *========================================================= *//** * class simpledocumentparser * use sax parse xml file, and build simpledocumentobject * all this pachage's is work for xml file, and method is action as dom. * * @package smartweb.common.xml * @version 1.0 */class simpledocumentparser{ private $domrootobject = null; private $currentno = null; private $currentname = null; private $currentvalue = null; private $currentattribute = null; public function getsimpledocument() { return $this->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); }}?>
<?php/** *========================================================= * * @author hahawen(大龄青年)* @since 2004-12-04 * @copyright copyright (c) 2004, nxcoder group * *========================================================= *//** * abstract class simpledocumentbase * base class for xml file parse * all this pachage's is work for xml file, and method is action as dom. * * 1/ add/update/remove data of xml file. * 2/ explode data to array. * 3/ rebuild xml file * * @package smartweb.common.xml * @abstract * @version 1.0 */abstract class simpledocumentbase{ private $nodetag = null; private $attributes = array(); private $values = array(); private $nodes = array(); function __construct($nodetag) { $this->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($pos<=0) { return $this->getnode($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 = "$prefixspace<$this->nodetag"; 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<$key"; foreach($value[attrs] as $attkey=>$attvalue) $str .= " $attkey=/"$attvalue/""; $tmpstr = $value[value]; } else { $str .= "$prefixspace/t<$key"; $tmpstr = $value; } $tmpstr = trim(trim($tmpstr, "/r/n")); $str .= ($tmpstr===null || $tmpstr==="")? " />/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); }}?>
<?php/** *========================================================= * * @author hahawen(大龄青年)* @since 2004-12-04 * @copyright copyright (c) 2004, nxcoder group * *========================================================= *//** * class simpledocumentroot * xml root class, include values/attributes/subnodes. * all this pachage's is work for xml file, and method is action as dom. * * @package smartweb.common.xml * @version 1.0 */class simpledocumentroot extends simpledocumentbase{ private $prefixstr = ''; private $nodelists = array(); function __construct($nodetag) { parent::__construct($nodetag); } public function createnodeobject($pnodeid, $name, $attributes) { $seq = sizeof($this->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); }}?>
<?php/** *========================================================= * * @author hahawen(大龄青年)* @since 2004-12-04 * @copyright copyright (c) 2004, nxcoder group * *========================================================= *//** * class simpledocumentnode * xml node class, include values/attributes/subnodes. * all this pachage's is work for xml file, and method is action as dom. * * @package smartweb.common.xml * @version 1.0 */class simpledocumentnode extends simpledocumentbase{ private $seq = null; private $rootobject = null; private $pnodeid = null; function __construct($rootobject, $pnodeid, $nodetag, $seq) { parent::__construct($nodetag); $this->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文件的内容
<?xml version="1.0" encoding="gb2312" ?><shop> <name>华联</name> <address>北京长安街-9999号</address> <desc>连锁超市</desc> <telphone>123456789</telphone> <cat id="food"> <goods id="food11"> <name>food11</name> <price>12.90</price> </goods> <goods id="food12"> <name>food12</name> <price>22.10</price> <desc creator="hahawen">好东西推荐</desc> </goods> </cat> <cat> <goods id="tel21"> <name>tel21</name> <price>1290</price> </goods> </cat> <cat id="coat"> <goods id="coat31"> <name>coat31</name> <price>112</price> </goods> <goods id="coat32"> <name>coat32</name> <price>45</price> </goods> </cat> <special id="hot"> <goods> <name>hot41</name> <price>99</price> </goods> </special></shop>
下面是通过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"添加属性, 然后显示添加后的结果
<?xml version="1.0" encoding="gb2312" ?><shop> <name>华联</name> <address>北京长安街-9999号</address> <desc>连锁超市</desc> <telphone>123456789</telphone> <cat id="food"> <goods id="food11"> <name>food11</name> <price>12.90</price> <leaveword author="hahawen" date="2004-12-05">这个商品不错</leaveword> </goods> <goods id="food12"> <name>food12</name> <price>22.10</price> <desc creator="hahawen">好东西推荐</desc> </goods> </cat> <cat> <goods id="tel21"> <name>tel21</name> <price>1290</price> </goods> </cat> <cat id="coat"> <goods id="coat31"> <name>coat31</name> <price>112</price> </goods> <goods id="coat32"> <name>coat32</name> <price>45</price> </goods> </cat> <special id="hot"> <goods> <name>hot41</name> <price>99</price> </goods> </special></shop>
下面是通过removevalue()/removeattribute()函数,给商品"food11"改变和删除属性, 然后显示操作后的结果
<?xml version="1.0" encoding="gb2312" ?><shop> <name>华联</name> <address>北京长安街-9999号</address> <desc>连锁超市</desc> <telphone>123456789</telphone> <cat id="food"> <goods id="food11"> <name>food11</name> <price>12.90</price> <leaveword author="hahawen" date="2004-12-05">这个商品不错</leaveword> </goods> <goods id="food12"> <name>new food12</name> <price>22.10</price> </goods> </cat> <cat> <goods id="tel21"> <name>tel21</name> <price>1290</price> </goods> </cat> <cat id="coat"> <goods id="coat31"> <name>coat31</name> <price>112</price> </goods> <goods id="coat32"> <name>coat32</name> <price>45</price> </goods> </cat> <special id="hot"> <goods> <name>hot41</name> <price>99</price> </goods> </special></shop>
下面是通过createnode()函数,添加商品, 然后显示添加后的结果
<?xml version="1.0" encoding="gb2312" ?><shop> <name>华联</name> <address>北京长安街-9999号</address> <desc>连锁超市</desc> <telphone>123456789</telphone> <cat id="food"> <goods id="food11"> <name>food11</name> <price>12.90</price> <leaveword author="hahawen" date="2004-12-05">这个商品不错</leaveword> </goods> <goods id="food12"> <name>new food12</name> <price>22.10</price> </goods> <goods id="food13"> <name>food13</name> <price>100</price> </goods> </cat> <cat> <goods id="tel21"> <name>tel21</name> <price>1290</price> </goods> </cat> <cat id="coat"> <goods id="coat31"> <name>coat31</name> <price>112</price> </goods> <goods id="coat32"> <name>coat32</name> <price>45</price> </goods> </cat> <special id="hot"> <goods> <name>hot41</name> <price>99</price> </goods> </special></shop>
下面是通过removenode()函数,删除商品, 然后显示删除后的结果
<?xml version="1.0" encoding="gb2312" ?><shop> <name>华联</name> <address>北京长安街-9999号</address> <desc>连锁超市</desc> <telphone>123456789</telphone> <cat id="food"> <goods id="food11"> <name>food11</name> <price>12.90</price> <leaveword author="hahawen" date="2004-12-05">这个商品不错</leaveword> </goods> <goods id="food13"> <name>food13</name> <price>100</price> </goods> </cat> <cat> <goods id="tel21"> <name>tel21</name> <price>1290</price> </goods> </cat> <cat id="coat"> <goods id="coat31"> <name>coat31</name> <price>112</price> </goods> <goods id="coat32"> <name>coat32</name> <price>45</price> </goods> </cat> <special id="hot"> <goods> <name>hot41</name> <price>99</price> </goods> </special></shop>
新闻热点
疑难解答