Use DOM in PHP5 to control XML (2) // write new data to 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 );
// Delete a node from the DOM
// $ Dom-> documentElement-> RemoveChild ($ dom-> documentElement-> getElementsByTagName ("channel")-> item (0 ));
// Alternatively, use xpath to query the nodes and then delete them.
// $ Dom-> documentElement-> RemoveChild ($ xpath-> query ("/rss/channel")-> item (0 ));
// $ Dom-> save ("newfile. xml ");
// Modify node data from the DOM
// Modify the file of the first title
// This place is stupid. create a new node and replace the old one. If any of your friends has other good methods, please tell me.
$ 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 );
// Modify attributes
// $ FirstTitle = $ xpath-> query ("/rss/channel/item/title")-> item (0 );
// $ FirstTitle-> setAttribute ("orderby", "4 ");
$ Dom-> save ("newfile. xml ");
Echo" View newfile. xml ";
// The following code retrieves and parses the php.net homepage and returns the content of the first title element.
/*
$ Dom-> loadHTMLFile ("http://www.php.net /");
$ Title = $ dom-> getElementsByTagName ("title ");
Print $ title-> item (0)-> textContent;
*/
?>
The code for the test. xml file is as follows:
Javascript
Http://blog.csdn.net/zhongmao/category/29515.aspx </link>
Javascript
Zh-chs
. Text version 0.958.2004.2001
Zhongmao
Out put excel used javascript
Http://blog.csdn.net/zhongmao/archive/2004/09/15/105385.aspx </link>
Wed, 15 sep 2004 13:32:00 gmt
The http://blog.csdn.net/zhongmao/archive/2004/09/15/105385.aspx </guid>
Http://blog.csdn.net/zhongmao/comments/105385.aspx </comment>
Http://blog.csdn.net/zhongmao/archive/2004/09/15/105385.aspx#feedback </comments>
2
A http://blog.csdn.net/zhongmao/comments/commentrss/105385.aspx </commentrss>
Http://blog.csdn.net/zhongmao/services/trackbacks/105385.aspx </ping>
Test description
Zhongmao
Out put word used javascript
Http://blog.csdn.net/zhongmao/archive/2004/08/06/67161.aspx </link>
Fri, 06 aug 2004 16:33:00 gmt
The http://blog.csdn.net/zhongmao/archive/2004/08/06/67161.aspx </guid>
Http://blog.csdn.net/zhongmao/comments/67161.aspx </comment>
Http://blog.csdn.net/zhongmao/archive/2004/08/06/67161.aspx#feedback </comments>
0
A http://blog.csdn.net/zhongmao/comments/commentrss/67161.aspx </commentrss>
Http://blog.csdn.net/zhongmao/services/trackbacks/67161.aspx </ping>
Test word description
Zhongmao
Xmlhttp
Http://blog.csdn.net/zhongmao/archive/2004/08/02/58417.aspx </link>
Mon, 02 aug 2004 10:11:00 gmt
The http://blog.csdn.net/zhongmao/archive/2004/08/02/58417.aspx </guid>
Http://blog.csdn.net/zhongmao/comments/58417.aspx </comment>
Http://blog.csdn.net/zhongmao/archive/2004/08/02/58417.aspx#feedback </comments>
0
A http://blog.csdn.net/zhongmao/comments/commentrss/58417.aspx </commentrss>
Http://blog.csdn.net/zhongmao/services/trackbacks/58417.aspx </ping>
Xmlhttpaaa asd bb cc dd
The preceding section uses DOM to control the content of XML (2) in PHP5. For more information, see PHP Chinese website (www.php1.cn )!