Use Dom to control XML implementation code in PhP5

Source: Internet
Author: User

The following example demonstrates Dom operations on XML. For more information, see Code Annotations in Copy code The 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 "<HR/> obtain all the title elements: <HR/> ";
$ Titles = $ dom-> getelementsbytagname ("title ");
Foreach ($ titles as $ node ){
Echo $ node-> textcontent. "<br/> ";
// You can also
// Echo $ node-> firstchild-> data. "<br/> ";
}

/*
Echo "<HR/> traverse all nodes from the root node: <br/> ";
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 "<HR/> result of the title node queried using XPath: <HR/> ";
$ 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 "<HR/> <a href = \" newfile. xml \ "> View newfile. xml </a> ";

// 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 code The 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>

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.