Use DOM to control XML in PHP5

Source: Internet
Author: User

Php (as the mainstream development language) 5 has enhanced the support for xml (standardization is getting closer and closer) and expanded xml (standardization is getting closer and closer) operation capabilities using DOM. These functions are part of the 5-core php (as the mainstream development language) and can be used without installation.

The following example demonstrates DOM operations on xml (standardization is getting closer and closer). For more information, see the comments in the code.

<?
/*************************************** *********
** Use xml (standardization is getting closer and closer) in php (as the mainstream development language) 5
** Reference site:
** Http://cn.php (as the mainstream development language). net/manual/zh/ref. dom. php (as the mainstream development language)
** The follow codes need php (as the mainstream development language) 5 support
**************************************** *********/


// Create a DOMDocument object first
$ Dom = new DomDocument ();
// Load the xml file (standardization is getting closer and closer ).
$ Dom-> load ("test. xml (standardization is getting closer and closer )");

// Output the xml file (standardization is getting closer and closer)
// Header ("Content-type: text/xml (standardization is getting closer and closer); charset = gb2312 ");
// Echo $ dom-> savexml (standardization is getting closer and closer )();

// Save the xml file (standardization is getting closer and closer), and the returned value is int (file size, in bytes)
// $ Dom-> save ("newfile. xml (standardization is getting closer and closer )");

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 ."";
}
}
}
}
*/

// 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]
/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 (standardization is getting closer and closer )");

// 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.

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.