用 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
<?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
<?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());
?>
文件:simpledocumentparser.php
<?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)
{
$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);
$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
<?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];
}
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);
}
}
?>
文件:simpledocumentroot.php
<?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);
}
}
?>
文件:simpledocumentnode.php
<?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>
注冊會員,創建你的web開發資料庫,