<p> 用 sax 方式的时候,要自己构建3个函数,而且要直接用这三的函数来返回数据,要求较强的逻辑。在处理不同结构的 xml 的时候,还要重新进行构造这三个函数,麻烦!</p><p> 用 dom 方式,倒是好些,但是他把每个节点都看作是一个 node,,操作起来要写好多的代码,麻烦!</p><p> 网上有好多的开源的 xml 解析的类库,以前看过几个,但是心里总是觉得不踏实,感觉总是跟在别人的屁股后面。</p><p> 这几天在搞 Java,挺累的,所以决定换换脑袋,写点 PHP 代码,为了防止以后 XML 解析过程再令我犯难,就花了一天的时间写了下面一个 XML 解析的类,于是就有了下面的东西。</p><p> 实现方式是通过包装“sax方式的解析结果”来实现的。总的来说,对于我个人来说挺实用的,性能也还可以,基本上可以完成大多数的处理要求。</p><p> 功能:</p><p> 1 对基本的 XML 文件的节点进行 查询 / 添加 / 修改 / 删除 工作。</p><p> 2 导出 XML 文件的所有数据到一个数组里面。</p><p> 3 整个设计采用了 OO 方式,在操作结果集的时候,使用方法类似于 dom</p><p> 缺点:</p><p> 1 每个节点最好都带有一个id(看后面的例子),每个“节点名字”=“节点的标签_节点的id”,如果这个 id 值没有设置,程序将自动给他产生一个 id,这个 id 就是这个节点在他的上级节点中的位置编号,从 0 开始。</p><p> 2 查询某个节点的时候可以通过用“|”符号连接“节点名字”来进行。这些“节点名字”都是按顺序写好的上级节点的名字。</p><p> 使用说明:</p><p> 运行下面的例子,在执行结果页面上可以看到函数的使用说明</p><p> 代码是通过 PHP5 来实现的,在 PHP4 中无法正常运行。</p><p> 由于刚刚写完,所以没有整理文档,下面的例子演示的只是一部分的功能,代码不是很难,要是想知道更多的功能,可以研究研究源代码。</p>
<p> </p>
<p> 目录结构:</p><p> test.php</p><p> test.xml</p><p> xml / SimpleDocumentBase.php</p><p> xml / SimpleDocumentNode.php</p><p> xml / SimpleDocumentRoot.php</p><p> xml / SimpleDocumentParser.php</p><p> 文件:test.xml</p><code><?xml version="1.0" encoding="GB2312"?><br /><shop><br /><name>华联</name><br /><address>北京长安街-9999号</address><br /><desc>连锁超市</desc><br /><cat id="food"><br /> <goods id="food11"><br /> <name>food11</name><br /> <price>12.90</price><br /> </goods><br /> <goods id="food12"><br /> <name>food12</name><br /> <price>22.10</price><br /> <desc creator="hahawen">好东西推荐</desc><br /> </goods><br /></cat><br /><cat><br /> <goods id="tel21"><br /> <name>tel21</name><br /> <price>1290</price><br /> </goods><br /></cat><br /><cat id="coat"><br /> <goods id="coat31"><br /> <name>coat31</name><br /> <price>112</price><br /> </goods><br /> <goods id="coat32"><br /> <name>coat32</name><br /> <price>45</price><br /> </goods><br /></cat><br /><special id="hot"><br /> <goods><br /> <name>hot41</name><br /> <price>99</price><br /> </goods><br /></special><br /></shop></code><p> 文件:test.php</p>
<p> </p>
<code><?php<br />require_once "xml/SimpleDocumentParser.php";<br />require_once "xml/SimpleDocumentBase.php";<br />require_once "xml/SimpleDocumentRoot.php";<br />require_once "xml/SimpleDocumentNode.php";<br />$test = new SimpleDocumentParser();<br />$test->parse("test.xml");<br />$dom = $test->getSimpleDocument();<br />echo "<pre>";<br />echo "<hr><font color=red>";<br />echo "下面是通过函数getSaveData()返回的整个xml数据的数组";<br />echo "</font><hr>";<br />print_r($dom->getSaveData());<br />echo "<hr><font color=red>";<br />echo "下面是通过setValue()函数,给给根节点添加信息,添加后显示出结果xml文件的内容";<br />echo "</font><hr>";<br />$dom->setValue("telphone", "123456789");<br />echo htmlspecialchars($dom->getSaveXml());<br />echo "<hr><font color=red>";<br />echo "下面是通过getNode()函数,返回某一个分类下的所有商品的信息";<br />echo "</font><hr>";<br />$obj = $dom->getNode("cat_food");<br />$nodeList = $obj->getNode();<br />foreach($nodeList as $node){<br /> $data = $node->getValue();<br /> echo "<font color=red>商品名:".$data[name]."</font><br>";<br /> print_R($data);<br /> print_R($node->getAttribute());<br />}<br />echo "<hr><font color=red>";<br />echo "下面是通过findNodeByPath()函数,返回某一商品的信息";<br />echo "</font><hr>";<br />$obj = $dom->findNodeByPath("cat_food|goods_food11");<br />if(!is_object($obj)){<br /> echo "该商品不存在";<br />}else{<br /> $data = $obj->getValue();<br /> echo "<font color=red>商品名:".$data[name]."</font><br>";<br /> print_R($data);<br /> print_R($obj->getAttribute());<br />}<br />echo "<hr><font color=red>";<br />echo "下面是通过setValue()函数,给商品"food11"添加属性, 然后显示添加后的结果";<br />echo "</font><hr>";<br />$obj = $dom->findNodeByPath("cat_food|goods_food11");<br />$obj->setValue("leaveword", array("value"=>"这个商品不错", "attrs"=>array("author"=>"hahawen", "date"=>date('Y-m-d'))));<br />echo htmlspecialchars($dom->getSaveXml());<br />echo "<hr><font color=red>";<br />echo "下面是通过removeValue()/removeAttribute()函数,给商品"food11"改变和删除属性, 然后显示操作后的结果";<br />echo "</font><hr>";<br />$obj = $dom->findNodeByPath("cat_food|goods_food12");<br />$obj->setValue("name", "new food12");<br />$obj->removeValue("desc");<br />echo htmlspecialchars($dom->getSaveXml());<br />echo "<hr><font color=red>";<br />echo "下面是通过createNode()函数,添加商品, 然后显示添加后的结果";<br />echo "</font><hr>";<br />$obj = $dom->findNodeByPath("cat_food");<br />$newObj = $obj->createNode("goods", array("id"=>"food13"));<br />$newObj->setValue("name", "food13");<br />$newObj->setValue("price", 100);<br />echo htmlspecialchars($dom->getSaveXml());<br />echo "<hr><font color=red>";<br />echo "下面是通过removeNode()函数,删除商品, 然后显示删除后的结果";<br />echo "</font><hr>";<br />$obj = $dom->findNodeByPath("cat_food");<br />$obj->removeNode("goods_food12");<br />echo htmlspecialchars($dom->getSaveXml());<br />?></code><p> 文件:SimpleDocumentParser.php</p>
<p> </p>
<code><?php<br />/**<br />*================================================<br />*<br />* @author hahawen(大龄青年) <br />* @since 2004-12-04<br />* @copyright Copyright (c) 2004, NxCoder Group<br />*<br />*================================================<br />*/<br />/**<br />* class SimpleDocumentParser<br />* use SAX parse xml file, and build SimpleDocumentObject<br />* all this pachage's is work for xml file, and method is action as DOM.<br />*<br />* @package SmartWeb.common.xml<br />* @version 1.0<br />*/<br />class SimpleDocumentParser<br />{<br /> private $domRootObject = null;<br /> private $currentNO = null;<br /> private $currentName = null;<br /> private $currentValue = null;<br /> private $currentAttribute = null;<br /> public<br /> function getSimpleDocument()<br /> {<br /> return $this->domRootObject;<br /> }<br /> publicfunction parse($file)<br /> {<br /> $xmlParser = xml_parser_create();<br /> xml_parser_set_option($xmlParser,XML_OPTION_CASE_FOLDING,<br /> 0);<br /> xml_parser_set_option($xmlParser,XML_OPTION_SKIP_WHITE, 1);<br /> xml_parser_set_option($xmlParser,<br /> XML_OPTION_TARGET_ENCODING, 'UTF-8');<br /> xml_set_object($xmlParser, $this);<br /> xml_set_element_handler($xmlParser, "startElement", "endElement");<br /> xml_set_character_data_handler($xmlParser,<br /> "characterData");<br /> if (!xml_parse($xmlParser, file_get_contents($file)))<br /> die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xmlParser)),<br /> xml_get_current_line_number($xmlParser)));<br /> xml_parser_free($xmlParser);<br /> }<br /> private function startElement($parser, $name, $attrs)<br /> {<br /> $this->currentName= $name;<br /> $this->currentAttribute = $attrs;<br /> if($this->currentNO == null)<br /> {<br /> $this->domRootObject = new SimpleDocumentRoot($name);<br /> $this->currentNO = $this->domRootObject;<br /> }<br /> else<br /> {<br /> $this->currentNO = $this->currentNO->createNode($name, $attrs);<br /> }<br /> }<br /> private function endElement($parser, $name)<br /> {<br /> if($this->currentName==$name)<br /> {<br /> $tag = $this->currentNO->getSeq();<br /> $this->currentNO = $this->currentNO->getPNodeObject();<br /> if($this->currentAttribute!=null && sizeof($this->currentAttribute)>0)<br /> $this->currentNO->setValue($name, array('value'=>$this->currentValue, 'attrs'=>$this->currentAttribute));<br /> else<br /> $this->currentNO->setValue($name, $this->currentValue);<br /> $this->currentNO->removeNode($tag);<br /> }<br /> else<br /> {<br /> $this->currentNO = (is_a($this->currentNO, 'SimpleDocumentRoot'))? null:<br /> $this->currentNO->getPNodeObject();<br /> }<br /> }<br /> private function characterData($parser, $data)<br /> {<br /> $this->currentValue = iconv('UTF-8', 'GB2312', $data);<br /> }<br /> function __destruct()<br /> {<br /> unset($this->domRootObject);<br /> }<br />}<br />?></code><p> 文件:SimpleDocumentBase.php</p>
<p> </p>
<code><?php<br />/**<br />*=================================================<br />*<br />* @author hahawen(大龄青年) <br />* @since 2004-12-04<br />* @copyright Copyright (c) 2004, NxCoder Group<br />*<br />*=================================================<br />*/<br />/**<br />* abstract class SimpleDocumentBase<br />* base class for xml file parse<br />* all this pachage's is work for xml file, and method is action as DOM.<br />*<br />* 1 add/update/remove data of xml file.<br />* 2 explode data to array.<br />* 3 rebuild xml file<br />*<br />* @package SmartWeb.common.xml<br />* @abstract<br />* @version 1.0<br />*/<br />abstract class SimpleDocumentBase<br />{<br /> private $nodeTag = null;<br /> private $attributes = array();<br /> private $values =<br /> array();<br /> private $nodes = array();<br /> function __construct($nodeTag)<br /> {<br /> $this->nodeTag = $nodeTag;<br /> }<br /> public function getNodeTag()<br /> {<br /> return $this->nodeTag;<br /> }<br /> public function setValues($values){<br /> $this->values = $values;<br /> }<br /> public function setValue($name, $value)<br /> {<br /> $this->values[$name] = $value;<br /> }<br /> public function getValue($name=null)<br /> {<br /> return $name==null?<br /> $this->values: $this->values[$name];<br /> }<br /> public function removeValue($name)<br /> {<br /> unset($this->values["$name"]);<br /> }<br /> public function setAttributes($attributes){<br /> $this->attributes = $attributes;<br /> }<br /> public function setAttribute($name, $value)<br /> {<br /> $this->attributes[$name] = $value;<br /> }<br /> public function getAttribute($name=null)<br /> {<br /> return $name==null? $this->attributes:<br /> $this->attributes[$name];<br /> }<br /> public function removeAttribute($name)<br /> {<br /> unset($this->attributes["$name"]);<br /> }<br /> public function getNodesSize()<br /> {<br /> return sizeof($this->nodes);<br /> }<br /> protected function setNode($name, $nodeId)<br /> {<br /> $this->nodes[$name]<br /> = $nodeId;<br /> }<br /> public abstract function createNode($name, $attributes);<br /> public abstract function removeNode($name);<br /> public abstract function getNode($name=null);<br /> protected function getNodeId($name=null)<br /> {<br /> return $name==null? $this->nodes: $this->nodes[$name];<br /> }<br /> protected function createNodeByName($rootNodeObj, $name, $attributes, $pId)<br /> {<br /> $tmpObject = $rootNodeObj->createNodeObject($pId, $name, $attributes);<br /> $key = isset($attributes[id])?<br /> $name.'_'.$attributes[id]: $name.'_'.$this->getNodesSize();<br /> $this->setNode($key, $tmpObject->getSeq());<br /> return $tmpObject;<br /> }<br /> protected function removeNodeByName($rootNodeObj, $name)<br /> {<br /> $rootNodeObj->removeNodeById($this->getNodeId($name));<br /> if(sizeof($this->nodes)==1)<br /> $this->nodes = array();<br /> else<br /> unset($this->nodes[$name]);<br /> }<br /> protected function getNodeByName($rootNodeObj, $name=null)<br /> {<br /> if($name==null)<br /> {<br /> $tmpList = array();<br /> $tmpIds = $this->getNodeId();<br /> foreach($tmpIds as $key=>$id)<br /> $tmpList[$key] = $rootNodeObj->getNodeById($id);<br /> return $tmpList;<br /> }<br /> else<br /> {<br /> $id = $this->getNodeId($name);<br /> if($id===null)<br /> {<br /> $tmpIds = $this->getNodeId();<br /> foreach($tmpIds as $tkey=>$tid)<br /> {<br /> if(strpos($key, $name)==0)<br /> {<br /> $id = $tid;<br /> break;<br /> }<br /> }<br /> }<br /> return $rootNodeObj->getNodeById($id);<br /> }<br /> }<br /> public function findNodeByPath($path)<br /> {<br /> $pos = strpos($path, '|');<br /> if($pos<=0)<br /> {<br /> return $this->getNode($path);<br /> }<br /> else<br /> {<br /> $tmpObj = $this->getNode(substr($path, 0,<br /> $pos));<br /> return is_object($tmpObj)?<br /> $tmpObj->findNodeByPath(substr($path,<br /> $pos+1)):<br /> null;<br /> }<br /> }<br /> public function getSaveData()<br /> {<br /> $data = $this->values;<br /> if(sizeof($this->attributes)>0)<br /> $data[attrs] = $this->attributes;<br /> $nodeList = $this->getNode();<br /> if($nodeList==null)<br /> return $data;<br /> foreach($nodeList as $key=>$node)<br /> {<br /> $data[$key] = $node->getSaveData();<br /> }<br /> return $data;<br /> }<br /> public function getSaveXml($level=0)<br /> {<br /> $prefixSpace<br /> = str_pad("",<br /> $level, " ");<br /> $str = "$prefixSpace<$this->nodeTag";<br /> foreach($this->attributes as $key=>$value)<br /> $str .= " $key="$value"";<br /> $str .= "><br />";<br /> foreach($this->values as $key=>$value){<br /> if(is_array($value))<br /> {<br /> $str .= "$prefixSpace <$key";<br /> foreach($value[attrs] as $attkey=>$attvalue)<br /> $str .= " $attkey="$attvalue"";<br /> $tmpStr = $value[value];<br /> }<br /> else<br /> {<br /> $str .= "$prefixSpace <$key";<br /> $tmpStr = $value;<br /> }<br /> $tmpStr = trim(trim($tmpStr, "<br />"));<br /> $str .= ($tmpStr===null || $tmpStr==="")? " /><br />": ">$tmpStr</$key><br />";<br /> }<br /> foreach($this->getNode() as $node)<br /> $str .= $node->getSaveXml($level+1)."<br />";<br /> $str .= "$prefixSpace</$this->nodeTag>";<br /> return $str;<br /> }<br /> function __destruct()<br /> {<br /> unset($this->nodes, $this->attributes, $this->values);<br /> }<br />}<br />?></code></p>
<p> </p>
<p> 文件:SimpleDocumentRoot.php</p><code><?php<br />/**<br />*==============================================<br />*<br />* @author hahawen(大龄青年) <br />* @since 2004-12-04<br />* @copyright Copyright (c) 2004, NxCoder Group<br />*<br />*==============================================<br />*/<br />/**<br />* class SimpleDocumentRoot<br />* xml root class, include values/attributes/subnodes.<br />* all this pachage's is work for xml file, and method is action as DOM.<br />*<br />* @package SmartWeb.common.xml<br />* @version 1.0<br />*/<br />class SimpleDocumentRoot extends SimpleDocumentBase<br />{<br /> private $prefixStr = '';<br /> private $nodeLists = array();<br /> function __construct($nodeTag)<br /> {<br /> parent::__construct($nodeTag);<br /> }<br /> public function createNodeObject($pNodeId, $name, $attributes)<br /> {<br /> $seq = sizeof($this->nodeLists);<br /> $tmpObject = new SimpleDocumentNode($this,<br /> $pNodeId, $name, $seq);<br /> $tmpObject->setAttributes($attributes);<br /> $this->nodeLists[$seq] = $tmpObject;<br /> return $tmpObject;<br /> }<br /> public function removeNodeById($id)<br /> {<br /> if(sizeof($this->nodeLists)==1)<br /> $this->nodeLists = array();<br /> else<br /> unset($this->nodeLists[$id]);<br /> }<br /> public function getNodeById($id)<br /> {<br /> return $this->nodeLists[$id];<br /> }<br /> public function createNode($name, $attributes)<br /> {<br /> return $this->createNodeByName($this, $name, $attributes, -1);<br /> }<br /> public function removeNode($name)<br /> {<br /> return $this->removeNodeByName($this, $name);<br /> }<br /> public function getNode($name=null)<br /> {<br /> return $this->getNodeByName($this, $name);<br /> }<br /> public function getSaveXml()<br /> {<br /> $prefixSpace = "";<br /> $str = $this->prefixStr."<br />";<br /> return $str.parent::getSaveXml(0);<br /> }<br />}<br />?></code><p> 文件:SimpleDocumentNode.php</p>
<p> </p>
<code><?php<br />/**<br />*===============================================<br />*<br />* @author hahawen(大龄青年) <br />* @since 2004-12-04<br />* @copyright Copyright (c) 2004, NxCoder Group<br />*<br />*===============================================<br />*/<br />/**<br />* class SimpleDocumentNode<br />* xml Node class, include values/attributes/subnodes.<br />* all this pachage's is work for xml file, and method is action as DOM.<br />*<br />* @package SmartWeb.common.xml<br />* @version 1.0<br />*/<br />class SimpleDocumentNode extends SimpleDocumentBase<br />{<br /> private $seq = null;<br /> private $rootObject = null;<br /> private $pNodeId = null;<br /> function __construct($rootObject, $pNodeId, $nodeTag, $seq)<br /> {<br /> parent::__construct($nodeTag);<br /> $this->rootObject = $rootObject;<br /> $this->pNodeId = $pNodeId;<br /> $this->seq = $seq;<br /> }<br /> public function getPNodeObject()<br /> {<br /> return ($this->pNodeId==-1)?<br /> $this->rootObject:<br /> $this->rootObject->getNodeById($this->pNodeId);<br /> }<br /> public function getSeq(){<br /> return $this->seq;<br /> }<br /> public function createNode($name, $attributes)<br /> {<br /> return $this->createNodeByName($this->rootObject,<br /> $name, $attributes,<br /> $this->getSeq());<br /> }<br /> public function removeNode($name)<br /> {<br /> return $this->removeNodeByName($this->rootObject, $name);<br /> }<br /> public function getNode($name=null)<br /> {<br /> return $this->getNodeByName($this->rootObject,<br /> $name);<br /> }<br />}<br />?></code></p>
<p> </p>
<p> 下面是例子运行对结果</p><p> 下面是通过函数getSaveData()返回的整个xml数据的数组<code>Array<br />(<br /> [name] => 华联<br /> [address] => 北京长安街-9999号<br /> [desc] => 连锁超市<br /> [cat_food] => Array<br /> (<br /> [attrs] => Array<br /> (<br /> [id] => food<br /> )<br /> [goods_food11] => Array<br /> (<br /> [name] => food11<br /> [price] => 12.90<br /> [attrs] => Array<br /> (<br /> [id] => food11<br /> )<br /> )<br /> [goods_food12] => Array<br /> (<br /> [name] => food12<br /> [price] => 22.10<br /> [desc] => Array<br /> (<br /> [value] => 好东西推荐<br /> [attrs] => Array<br /> (<br /> [creator] => hahawen<br /> )<br /> )<br /> [attrs] => Array<br /> (<br /> [id] => food12<br /> )<br /> )<br /> )<br /> [cat_1] => Array<br /> (<br /> [goods_tel21] => Array<br /> (<br /> [name] => tel21<br /> [price] => 1290<br /> [attrs] => Array<br /> (<br /> [id] => tel21<br /> )<br /> )<br /> )<br /> [cat_coat] => Array<br /> (<br /> [attrs] => Array<br /> (<br /> [id] => coat<br /> )<br /> [goods_coat31] => Array<br /> (<br /> [name] => coat31<br /> [price] => 112<br /> [attrs] => Array<br /> (<br /> [id] => coat31<br /> )<br /> )<br /> [goods_coat32] => Array<br /> (<br /> [name] => coat32<br /> [price] => 45<br /> [attrs] => Array<br /> (<br /> [id] => coat32<br /> )<br /> )<br /> )<br /> [special_hot] => Array<br /> (<br /> [attrs] => Array<br /> (<br /> [id] => hot<br /> )<br /> [goods_0] => Array<br /> (<br /> [name] => hot41<br /> [price] => 99<br /> )<br /> )<br />)</code></p>
<p> </p>
<p> 下面是通过setValue()函数,给给根节点添加信息,添加后显示出结果xml文件的内容<code><?xml version="1.0" encoding="GB2312" ?><br /><shop><br /><name>华联</name><br /><address>北京长安街-9999号</address><br /><desc>连锁超市</desc><br /><telphone>123456789</telphone><br /><cat id="food"><br /> <goods id="food11"><br /> <name>food11</name><br /> <price>12.90</price><br /> </goods><br /> <goods id="food12"><br /> <name>food12</name><br /> <price>22.10</price><br /> <desc creator="hahawen">好东西推荐</desc><br /> </goods><br /></cat><br /><cat><br /> <goods id="tel21"><br /> <name>tel21</name><br /> <price>1290</price><br /> </goods><br /></cat><br /><cat id="coat"><br /> <goods id="coat31"><br /> <name>coat31</name><br /> <price>112</price><br /> </goods><br /> <goods id="coat32"><br /> <name>coat32</name><br /> <price>45</price><br /> </goods><br /></cat><br /><special id="hot"><br /> <goods><br /> <name>hot41</name><br /> <price>99</price><br /> </goods><br /></special><br /></shop></code>下面是通过getNode()函数,返回某一个分类下的所有商品的信息 商品名:food11</p>
<p> </p>
<code>Array<br />(<br /> [name] => food11<br /> [price] => 12.90<br />)<br />Array<br />(<br /> [id] => food11<br />)</code></p><p> 商品名:food12</p><code>Array<br />(<br /> [name] => food12<br /> [price] => 22.10<br /> [desc] => Array<br /> (<br /> [value] => 好东西推荐<br /> [attrs] => Array<br /> (<br /> [creator] => hahawen<br /> )<br /> )<br />)<br />Array<br />(<br /> [id] => food12<br />)</code></p><p> 下面是通过findNodeByPath()函数,返回某一商品的信息 商品名:food11</p><code>Array<br />(<br /> [name] => food11<br /> [price] => 12.90<br />)<br />Array<br />(<br /> [id] => food11<br />)</code></p><p> 下面是通过setValue()函数,给商品"food11"添加属性, 然后显示添加后的结果<code><?xml version="1.0" encoding="GB2312" ?><br /><shop><br /><name>华联</name><br /><address>北京长安街-9999号</address><br /><desc>连锁超市</desc><br /><telphone>123456789</telphone><br /><cat id="food"><br /> <goods id="food11"><br /> <name>food11</name><br /> <price>12.90</price><br /> <leaveword author="hahawen" date="2004-12-05">这个商品不错</leaveword><br /> </goods><br /> <goods id="food12"><br /> <name>food12</name><br /> <price>22.10</price><br /> <desc creator="hahawen">好东西推荐</desc><br /> </goods><br /></cat><br /><cat><br /> <goods id="tel21"><br /> <name>tel21</name><br /> <price>1290</price><br /> </goods><br /></cat><br /><cat id="coat"><br /> <goods id="coat31"><br /> <name>coat31</name><br /> <price>112</price><br /> </goods><br /> <goods id="coat32"><br /> <name>coat32</name><br /> <price>45</price><br /> </goods><br /></cat><br /><special id="hot"><br /> <goods><br /> <name>hot41</name><br /> <price>99</price><br /> </goods><br /></special><br /></shop> 下面是通过removeValue()/removeAttribute()函数,给商品"food11"改变和删除属性, 然后显示操作后的结果 <?xml version="1.0" encoding="GB2312" ?><br /><shop><br /><name>华联</name><br /><address>北京长安街-9999号</address><br /><desc>连锁超市</desc><br /><telphone>123456789</telphone><br /><cat id="food"><br /> <goods id="food11"><br /> <name>food11</name><br /> <price>12.90</price><br /> <leaveword author="hahawen" date="2004-12-05">这个商品不错</leaveword><br /> </goods><br /> <goods id="food12"><br /> <name>new food12</name><br /> <price>22.10</price><br /> </goods><br /></cat><br /><cat><br /> <goods id="tel21"><br /> <name>tel21</name><br /> <price>1290</price><br /> </goods><br /></cat><br /><cat id="coat"><br /> <goods id="coat31"><br /> <name>coat31</name><br /> <price>112</price><br /> </goods><br /> <goods id="coat32"><br /> <name>coat32</name><br /> <price>45</price><br /> </goods><br /></cat><br /><special id="hot"><br /> <goods><br /> <name>hot41</name><br /> <price>99</price><br /> </goods><br /></special><br /></shop> 下面是通过createNode()函数,添加商品, 然后显示添加后的结果 <?xml version="1.0" encoding="GB2312" ?><br /><shop><br /><name>华联</name><br /><address>北京长安街-9999号</address><br /><desc>连锁超市</desc><br /><telphone>123456789</telphone><br /><cat id="food"><br /> <goods id="food11"><br /> <name>food11</name><br /> <price>12.90</price><br /> <leaveword author="hahawen" date="2004-12-05">这个商品不错</leaveword><br /> </goods><br /> <goods id="food12"><br /> <name>new food12</name><br /> <price>22.10</price><br /> </goods><br /> <goods id="food13"><br /> <name>food13</name><br /> <price>100</price><br /> </goods><br /></cat><br /><cat><br /> <goods id="tel21"><br /> <name>tel21</name><br /> <price>1290</price><br /> </goods><br /></cat><br /><cat id="coat"><br /> <goods id="coat31"><br /> <name>coat31</name><br /> <price>112</price><br /> </goods><br /> <goods id="coat32"><br /> <name>coat32</name><br /> <price>45</price><br /> </goods><br /></cat><br /><special id="hot"><br /> <goods><br /> <name>hot41</name><br /> <price>99</price><br /> </goods><br /></special><br /></shop> 下面是通过removeNode()函数,删除商品, 然后显示删除后的结果 <?xml version="1.0" encoding="GB2312" ?><br /><shop><br /><name>华联</name><br /><address>北京长安街-9999号</address><br /><desc>连锁超市</desc><br /><telphone>123456789</telphone><br /><cat id="food"><br /> <goods id="food11"><br /> <name>food11</name><br /> <price>12.90</price><br /> <leaveword author="hahawen" date="2004-12-05">这个商品不错</leaveword><br /> </goods><br /> <goods id="food13"><br /> <name>food13</name><br /> <price>100</price><br /> </goods><br /></cat><br /><cat><br /> <goods id="tel21"><br /> <name>tel21</name><br /> <price>1290</price><br /> </goods><br /></cat><br /><cat id="coat"><br /> <goods id="coat31"><br /> <name>coat31</name><br /> <price>112</price><br /> </goods><br /> <goods id="coat32"><br /> <name>coat32</name><br /> <price>45</price><br /> </goods><br /></cat><br /><special id="hot"><br /> <goods><br /> <name>hot41</name><br /> <price>99</price><br /> </goods><br /></special><br /></shop></code></p>