国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

使用PHP 5.0 輕松解析XML文檔

2024-05-04 23:03:57
字體:
供稿:網(wǎng)友


收集最實(shí)用的網(wǎng)頁特效代碼!

用sax方式的時候,要自己構(gòu)建3個函數(shù),而且要直接用這三的函數(shù)來返回數(shù)據(jù), 要求較強(qiáng)的邏輯。 在處理不同結(jié)構(gòu)的xml的時候, 還要重新進(jìn)行構(gòu)造這三個函數(shù),麻煩!

用dom方式,倒是好些,但是他把每個節(jié)點(diǎn)都看作是一個node,操作起來要寫好多的代碼, 麻煩!

網(wǎng)上有好多的開源的xml解析的類庫, 以前看過幾個,但是心里總是覺得不踏實(shí),感覺總是跟在別人的屁股后面.

這幾天在搞java, 挺累的,所以決定換換腦袋,寫點(diǎn)php代碼,為了防止以后xml解析過程再令我犯難,就花了一天的時間寫了下面一個xml解析的類,于是就有了下面的東西。

實(shí)現(xiàn)方式是通過包裝"sax方式的解析結(jié)果"來實(shí)現(xiàn)的. 總的來說,對于我個人來說挺實(shí)用的,性能也還可以,基本上可以完成大多數(shù)的處理要求。

功能:

1、 對基本的xml文件的節(jié)點(diǎn)進(jìn)行 查詢 / 添加 / 修改 / 刪除 工作.

2、導(dǎo)出xml文件的所有數(shù)據(jù)到一個數(shù)組里面.

3、整個設(shè)計采用了oo方式,在操作結(jié)果集的時候, 使用方法類似于dom

缺點(diǎn):

1、 每個節(jié)點(diǎn)最好都帶有一個id(看后面的例子), 每個“節(jié)點(diǎn)名字”=“節(jié)點(diǎn)的標(biāo)簽_節(jié)點(diǎn)的id”,如果這個id值沒有設(shè)置,程序?qū)⒆詣咏o他產(chǎn)生一個id,這個id就是這個節(jié)點(diǎn)在他的上級節(jié)點(diǎn)中的位置編號,從0開始。

2、 查詢某個節(jié)點(diǎn)的時候可以通過用“|”符號連接“節(jié)點(diǎn)名字”來進(jìn)行。這些“節(jié)點(diǎn)名字”都是按順序?qū)懞玫纳霞壒?jié)點(diǎn)的名字。

使用說明:

運(yùn)行下面的例子,在執(zhí)行結(jié)果頁面上可以看到函數(shù)的使用說明

代碼是通過php5來實(shí)現(xiàn)的,在php4中無法正常運(yùn)行。

由于剛剛寫完,所以沒有整理文檔,下面的例子演示的只是一部分的功能,代碼不是很難,要是想知道更多的功能,可以研究研究源代碼。

目錄結(jié)構(gòu):

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>華聯(lián)</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 "下面是通過函數(shù)getsavedata()返回的整個xml數(shù)據(jù)的數(shù)組"; echo "</font><hr>"; print_r($dom->getsavedata()); echo "<hr><font color=red>"; echo "下面是通過setvalue()函數(shù),給給根節(jié)點(diǎn)添加信息,添加后顯示出結(jié)果xml文件的內(nèi)容"; echo "</font><hr>"; $dom->setvalue("telphone", "123456789"); echo htmlspecialchars($dom->getsavexml()); echo "<hr><font color=red>"; echo "下面是通過getnode()函數(shù),返回某一個分類下的所有商品的信息"; 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()函數(shù),返回某一商品的信息"; 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()函數(shù),給商品/"food11/"添加屬性, 然后顯示添加后的結(jié)果"; 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()函數(shù), 給商品/"food11/"改變和刪除屬性, 然后顯示操作后的結(jié)果"; 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()函數(shù),添加商品, 然后顯示添加后的結(jié)果"; 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()函數(shù),刪除商品, 然后顯示刪除后的結(jié)果"; 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 = '<?xml version="1.0" encoding="utf-8" ?>';

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);

}

}

?>

下面是例子運(yùn)行對結(jié)果:

下面是通過函數(shù)getsavedata()返回的整個xml數(shù)據(jù)的數(shù)組

array

(

[name] => 華聯(lián)

[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()函數(shù),給給根節(jié)點(diǎn)添加信息,添加后顯示出結(jié)果xml文件的內(nèi)容

<?xml version="1.0" encoding="gb2312" ?>

<shop>

<name>華聯(lián)</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>

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 杭州市| 孟连| 舒兰市| 广汉市| 湟源县| 林西县| 卓资县| 萨嘎县| 凤庆县| 红安县| 游戏| 砀山县| 巨野县| 南漳县| 湘潭县| 秦安县| 遂平县| 湖州市| 北安市| 南和县| 华容县| 华亭县| 清苑县| 运城市| 开江县| 英超| 浦东新区| 壤塘县| 岗巴县| 那坡县| 梧州市| 江北区| 连州市| 齐齐哈尔市| 黄浦区| 宁陵县| 华坪县| 安岳县| 浪卡子县| 策勒县| 灌阳县|