Use DOM to control XML in PHP5 (2)

Source: Internet
Author: User
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 )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.