The following example demonstrates DOM operations on XML. For more information, see the comments in the code.
Copy codeThe Code is as follows:
<?
/*************************************** *********
** Use XML in PHP5
** Reference site:
** Http://cn.php.net/manual/zh/ref.dom.php
** The follow codes need PHP5 support
**************************************** *********/
// Create a DOMDocument object first
$ Dom = new DomDocument ();
// Load the XML file
$ Dom-> load ("test. xml ");
// Output the XML file
// Header ("Content-type: text/xml; charset = gb2312 ");
// Echo $ dom-> saveXML ();
// Save the XML file. The returned value is int (file size, in bytes)
// $ Dom-> save ("newfile. xml ");
Echo "$ Titles = $ dom-> getElementsByTagName ("title ");
Foreach ($ titles as $ node ){
Echo $ node-> textContent. "<br/> ";
// You can also
// Echo $ node-> firstChild-> data. "<br/> ";
}
/*
Echo "Foreach ($ dom-> documentElement-> childNodes as $ items ){
// If the node is an element (nodeType = 1) and its name is item, the loop continues.
If ($ items-> nodeType = 1 & $ items-> nodeName = "item "){
Foreach ($ items-> childNodes as $ titles ){
// If a node is an element and its name is title, print it.
If ($ titles-> nodeType = 1 & $ titles-> nodeName = "title "){
Print $ titles-> textContent. "\ n ";
}
}
}
}
*/
// Use XPath to query data
Echo "$ Xpath = new domxpath ($ dom );
$ Titles = $ xpath-> query ("/rss/channel/item/title ");
Foreach ($ titles as $ node ){
Echo $ node-> textContent. "<br/> ";
}
/*
This is similar to using the getElementsByTagName () method, but Xpath is much more powerful.
In depth, it may be like this:
/Rss/channel/item [position () = 1]/title returns all
/Rss/channel/item/title [@ id = '23'] return all the titles that contain the id attribute and whose value is 23.
/Rss/channel/& folder &/title returns the title under all articles elements)
*/
// Write 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 );
// 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 "
// 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:
Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "gb2312"?>
& Lt; rss version = "2.0" & gt;
<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>