您好,欢迎访问一九零五行业门户网

PHP5中使用DOM控制XML实现代码_PHP教程

下面的例子简单的演示了dom对xml的操作,详细解释请看代码中的注释
<? /************************************************ ** use xml in php5 ** reference site: ** http://cn.php.net/manual/zh/ref.dom.php ** the follow codes need php5 support *************************************************/ //首先要创建一个domdocument对象 $dom = new domdocument(); //然后载入xml文件 $dom -> load("test.xml"); //输出xml文件 //header("content-type: text/xml;charset=gb2312"); //echo $dom -> savexml(); //保存xml文件,返回值为int(文件大小,以字节为单位) //$dom -> save("newfile.xml"); echo "<hr/>取得所有的title元素:<hr/>"; $titles = $dom -> getelementsbytagname("title"); foreach ($titles as $node){ echo $node -> textcontent . "<br/>"; //这样也可以 //echo $node->firstchild->data . "<br/>"; } /* echo "<hr/>从根结点遍历所有结点:<br/>"; foreach ($dom->documentelement->childnodes as $items) { //如果节点是一个元素(nodetype == 1)并且名字是item就继续循环 if ($items->nodetype == 1 && $items->nodename == "item") { foreach ($items->childnodes as $titles) { //如果节点是一个元素,并且名字是title就打印它. if ($titles->nodetype == 1 && $titles->nodename == "title") { print $titles->textcontent . "\n"; } } } } */ //使用xpath查询数据 echo "<hr/>使用xpath查询的title节点结果:<hr/>"; $xpath = new domxpath($dom); $titles = $xpath->query("/rss/channel/item/title"); foreach ($titles as $node){ echo $node->textcontent."<br/>"; } /* 这样和使用getelementsbytagname()方法差不多,但是xpath要强大的多 深入一点可能是这样: /rss/channel/item[position() = 1]/title 返回第一个item元素的所有 /rss/channel/item/title[@id = '23'] 返回所有含有id属性并且值为23的title /rss/channel/&folder&/title 返回所有articles元素下面的title(译者注:&folder&代表目录深度) */ //向dom中写入新数据 $item = $dom->createelement("item"); $title = $dom->createelement("title"); $titletext = $dom->createtextnode("title text"); $title->appendchild($titletext); $item->appendchild($title); $dom->documentelement->getelementsbytagname('channel')->item(0)->appendchild($item); //从dom中删除节点 //$dom->documentelement->removechild($dom->documentelement->getelementsbytagname("channel")->item(0)); //或者使用xpath查询出节点再删除 //$dom->documentelement->removechild($xpath->query("/rss/channel")->item(0)); //$dom->save("newfile.xml"); //从dom中修改节点数据 //修改第一个title的文件 //这个地方比较笨,新创建一个节点,然后替换旧的节点。如果哪位朋友有其他好的方法请一定要告诉我 $firsttitle = $xpath->query("/rss/channel/item/title")->item(0); $newtitle = $dom->createelement("title"); $newtitle->appendchild(new domtext("this's the new title text!!!")); $firsttitle->parentnode->replacechild($newtitle, $firsttitle); //修改属性 //$firsttitle = $xpath->query("/rss/channel/item/title")->item(0); //$firsttitle->setattribute("orderby", "4"); $dom->save("newfile.xml"); echo "<hr/><a href=\"newfile.xml\">查看newfile.xml</a>"; //下面的代码获得并解析php.net的首页,将返第一个title元素的内容。 /* $dom->loadhtmlfile("http://www.php.net/"); $title = $dom->getelementsbytagname("title"); print $title->item(0)->textcontent; */ ?>
下面是test.xml文件代码:
<?xml version="1.0" encoding="gb2312"?> <rss version="2.0"> <channel> <title>javascript</title> <link>http://blog.csdn.net/zhongmao/category/29515.aspx</link> <description>javascript</description> <language>zh-chs</language> <generator>.text version 0.958.2004.2001</generator> <item> <creator>zhongmao</creator> <title orderby="1">out put excel used javascript</title> <link>http://blog.csdn.net/zhongmao/archive/2004/09/15/105385.aspx</link> <pubdate>wed, 15 sep 2004 13:32:00 gmt</pubdate> <guid>http://blog.csdn.net/zhongmao/archive/2004/09/15/105385.aspx</guid> <comment>http://blog.csdn.net/zhongmao/comments/105385.aspx</comment> <comments>http://blog.csdn.net/zhongmao/archive/2004/09/15/105385.aspx#feedback</comments> <comments>2</comments> <commentrss>http://blog.csdn.net/zhongmao/comments/commentrss/105385.aspx</commentrss> <ping>http://blog.csdn.net/zhongmao/services/trackbacks/105385.aspx</ping> <description>test description</description> </item> <item> <creator>zhongmao</creator> <title orderby="2">out put word used javascript</title> <link>http://blog.csdn.net/zhongmao/archive/2004/08/06/67161.aspx</link> <pubdate>fri, 06 aug 2004 16:33:00 gmt</pubdate> <guid>http://blog.csdn.net/zhongmao/archive/2004/08/06/67161.aspx</guid> <comment>http://blog.csdn.net/zhongmao/comments/67161.aspx</comment> <comments>http://blog.csdn.net/zhongmao/archive/2004/08/06/67161.aspx#feedback</comments> <comments>0</comments> <commentrss>http://blog.csdn.net/zhongmao/comments/commentrss/67161.aspx</commentrss> <ping>http://blog.csdn.net/zhongmao/services/trackbacks/67161.aspx</ping> <description>test word description</description> </item> <item> <creator>zhongmao</creator> <title orderby="3">xmlhttp</title> <link>http://blog.csdn.net/zhongmao/archive/2004/08/02/58417.aspx</link> <pubdate>mon, 02 aug 2004 10:11:00 gmt</pubdate> <guid>http://blog.csdn.net/zhongmao/archive/2004/08/02/58417.aspx</guid> <comment>http://blog.csdn.net/zhongmao/comments/58417.aspx</comment> <comments>http://blog.csdn.net/zhongmao/archive/2004/08/02/58417.aspx#feedback</comments> <comments>0</comments> <commentrss>http://blog.csdn.net/zhongmao/comments/commentrss/58417.aspx</commentrss> <ping>http://blog.csdn.net/zhongmao/services/trackbacks/58417.aspx</ping> <description>xmlhttpaaa asd bb cc dd</description> </item> </channel> </rss>
以上就是php5中使用dom控制xml实现代码_php教程的内容。
其它类似信息

推荐信息