例如如下xml文件格式
<?xml version="1.0" encoding="utf-8"?> <appstore> <goods goods_id="j001"> <name>天龙八部</name> <intro><![CDATA[天龙八部是部好书]]></intro> </goods> <goods goods_id="j002"> <name>七龙珠</name> <intro><![CDATA[七龙珠动画片]]></intro> </goods> </appstore>
?PHP //1.创建DOM对象 $dom=new DOMDocument('1.0','utf-8'); //载入xml $dom->load('03.xml'); //把天龙八部的商品删掉 //查找goods节点,得到列表,从列表中选取天龙八部节点 $tl=$dom->getElementsByTagName('goods')->item(0); $tl->parentNode->removeChild($tl); //删除节点已经搞定 header('content-type:text/xml;'); echo $dom->savexml(); ?>
//修改节点,节点不能修改,只能替换 $name=$dom->getElementsByTagName('name')->item(0); $seven=$dom->createTextNode('七种武器'); $name->replaceChild($seven,$name->firstChild); header('content-type:text/xml;'); echo $dom->savexml();