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>
<?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());
?>
新闻热点
疑难解答