用 PHP5 轻松解析 XML
用 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.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>
文件:test.php
require_once "xml/simpledocumentparser.php";
require_once "xml/simpledocumentbase.php";
require_once "xml/simpledocumentroot.php";
require_once "xml/simpledocumentnode.php";
$test->parse("test.xml");
$dom = $test->getsimpledocument();
echo "下面是通过函数getsavedata()返回的整个xml数据的数组";
echo "</font><hr>";
print_r($dom->getsavedata());
echo "下面是通过setvalue()函数,给给根节点添加信息,添加后显示出结果xml文件的内容";
echo "</font><hr>";
$dom->setvalue("telphone", "123456789");
echo htmlspecialchars($dom->getsavexml());
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 "下面是通过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 "下面是通过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 "下面是通过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 "下面是通过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 "下面是通过removenode()函数,删除商品, 然后显示删除后的结果";
echo "</font><hr>";
$obj = $dom->findnodebypath("cat_food");
$obj->removenode("goods_food12");
echo htmlspecialchars($dom->getsavexml());
?>
/**
*================================================
*
* @author hahawen(大龄青年)
* @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 $currentname = null;
private $currentvalue = null;
private $currentattribute = null;
function getsimpledocument()
{
return $this->domrootobject;
}
{
$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_character_data_handler($xmlparser,
"characterdata");
xml_get_current_line_number($xmlparser)));
{
$this->currentname = $name;
$this->currentattribute = $attrs;
if($this->currentno == null)
{
$this->domrootobject = new simpledocumentroot($name);
}
else
{
$this->currentno = $this->currentno->createnode($name, $attrs);
}
{
if($this->currentname==$name)
$tag = $this->currentno->getseq();
$this->currentno = $this->currentno->getpnodeobject();
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);
}
else
{
$this->currentno = (is_a($this->currentno, 'simpledocumentroot'))? null:
$this->currentno->getpnodeobject();
}
}
{
$this->currentvalue = iconv('utf-8', 'gb2312', $data);
}
function __destruct()
{
unset($this->domrootobject);
}
?>
/**
*=================================================
*
* @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 $values =
array();
{
$this->nodetag = $nodetag;
}
{
return $this->nodetag;
}
$this->values = $values;
}
{
$this->values[$name] = $value;
}
{
return $name==null?
$this->values: $this->values[$name];
}
{
unset($this->values["$name"]);
}
$this->attributes = $attributes;
}
{
$this->attributes[$name] = $value;
}
{
return $name==null? $this->attributes:
$this->attributes[$name];
}
{
unset($this->attributes["$name"]);
}
{
return sizeof($this->nodes);
}
{
$this->nodes[$name]
= $nodeid;
}
{
return $name==null? $this->nodes: $this->nodes[$name];
}
{
$tmpobject = $rootnodeobj->createnodeobject($pid, $name, $attributes);
$key = isset($attributes[id])?
$name.'_'.$attributes[id]: $name.'_'.$this->getnodessize();
$this->setnode($key, $tmpobject->getseq());
return $tmpobject;
}
{
$rootnodeobj->removenodebyid($this->getnodeid($name));
if(sizeof($this->nodes)==1)
$this->nodes = array();
else
unset($this->nodes[$name]);
}
{
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();
{
if(strpos($key, $name)==0)
{
$id = $tid;
break;
}
}
}
return $rootnodeobj->getnodebyid($id);
}
}
{
$pos = strpos($path, '|');
if($pos<=0)
{
return $this->getnode($path);
}
else
{
$pos));
$tmpobj->findnodebypath(substr($path,
$pos+1)):
null;
}
}
{
$data = $this->values;
if(sizeof($this->attributes)>0)
$nodelist = $this->getnode();
if($nodelist==null)
foreach($nodelist as $key=>$node)
{
$data[$key] = $node->getsavedata();
}
}
public function getsavexml($level=0)
{
= str_pad("",
$level, "\t");
$str = "$prefixspace<$this->nodetag";
$str .= " $key=\"$value\"";
foreach($this->values as $key=>$value){
{
$str .= "$prefixspace\t<$key";
}
else
$str .= "$prefixspace\t<$key";
}
$tmpstr = trim(trim($tmpstr, "\r\n"));
$str .= $node->getsavexml($level+1)."\r\n";
$str .= "$prefixspace</$this->nodetag>";
}
{
unset($this->nodes, $this->attributes, $this->values);
?>
/**
*==============================================
*
* @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
*/
{
private $prefixstr = '';
private $nodelists = array();
{
parent::__construct($nodetag);
}
{
$seq = sizeof($this->nodelists);
$tmpobject = new simpledocumentnode($this,
$pnodeid, $name, $seq);
$tmpobject->setattributes($attributes);
return $tmpobject;
}
{
if(sizeof($this->nodelists)==1)
$this->nodelists = array();
else
unset($this->nodelists[$id]);
}
{
return $this->nodelists[$id];
}
{
return $this->createnodebyname($this, $name, $attributes, -1);
}
{
return $this->removenodebyname($this, $name);
}
{
return $this->getnodebyname($this, $name);
}
{
$prefixspace = "";
$str = $this->prefixstr."\r\n";
return $str.parent::getsavexml(0);
}
}
?>
/**
*===============================================
*
* @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;
{
parent::__construct($nodetag);
$this->rootobject = $rootobject;
$this->pnodeid = $pnodeid;
$this->seq = $seq;
}
{
return ($this->pnodeid==-1)?
$this->rootobject:
$this->rootobject->getnodebyid($this->pnodeid);
}
return $this->seq;
}
{
return $this->createnodebyname($this->rootobject,
$name, $attributes,
$this->getseq());
}
{
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>
上一篇: 深入浅析Android接口回调机制