Use DOM to control XML in PHP5. 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 have enhanced xml (standardization is getting closer and closer) support in php (as the mainstream development language) 5 and extended xml (standardization is getting closer and closer) using DOM) operation capabilities. 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"
Obtain all the title elements: ";
$ Titles = $ dom-> getElementsByTagName ("title ");
Foreach ($ titles as $ node)
{
Echo $ node-> textContent ."
";
// You can also
// Echo $ node-> firstChild-> data ."
";
}
/*
Echo"
Traverse all nodes from the root node:
";
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"
The title node result queried using XPath: ";
$ Xpath = new domxpath ($ dom );
$ Titles = $ xpath-> query ("/rss/channel/item/title ");
Foreach ($ titles as $ node)
{
Echo $ node-> textContent ."
";
}
/*
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.
Http://www.bkjia.com/PHPjc/508732.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/508732.htmlTechArticlephp (as the current mainstream development language) 5 has enhanced the xml (standardization is getting closer and closer) support, the use of DOM to extend the xml (standardization is getting closer and closer) operation capabilities. These functions...