Home >php教程 >php手册 >使用PHP 5.0 轻松解析XML文档(5)

使用PHP 5.0 轻松解析XML文档(5)

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-21 09:15:221001browse

xml

下面是通过getNode()函数,返回某一个分类下的所有商品的信息商品名:

food11<br>Array(    [name] => food11    [price] => 12.90)Array(    [id] => food11)商品名:food12<br>Array(    [name] => food12    [price] => 22.10    [desc] => Array        (            [value] => 好东西推荐            [attrs] => Array                (                    [creator] => hahawen                )        ))Array(    [id] => food12)
  下面是通过findNodeByPath()函数,返回某一商品的信息商品名:
food11<br><br>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>



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn