Writing new data to the 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);
Remove a node from the DOM
$dom->documentelement->removechild ($dom->documentelement->getelementsbytagname ("channel") Item (0));
Or use XPath to query out nodes and then delete
$dom->documentelement->removechild ($xpath->query ("/rss/channel")->item (0));
$dom->save ("Newfile.xml");
Modifying node data from the DOM
Modify the file for the first title
This place is stupid, create a new node, and replace the old node. If a friend has any other good ways, please tell me.
$firstTitle = $xpath->query ("/rss/channel/item/title")->item (0);
$newTitle = $dom->createelement ("title");
$newTitle->appendchild (New Domtext ("This ' s new title text!!!"));
$firstTitle->parentnode->replacechild ($newTitle, $firstTitle);
modifying properties
$firstTitle = $xpath->query ("/rss/channel/item/title")->item (0);
$firstTitle->setattribute ("by", "4");
$dom->save ("Newfile.xml");
echo "
The following code obtains and parses the first page of the php.net, returning the contents of the title element.
/*
$dom->loadhtmlfile ("http://www.php.net/");
$title = $dom->getelementsbytagname ("title");
Print $title->item (0)->textcontent;
*/
?>
Here is the Test.xml file code:
<?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, 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, 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, 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>
The above is the use of Dom control in PHP5 XML (2) content, more related articles please pay attention to topic.alibabacloud.com (www.php.cn)!