Using DOM to control XML implementation code in PHP5

Source: Internet
Author: User
Tags comments file size generator xpath


<?xml version= "1.0" encoding= "gb2312"?>
<rss version= "2.0" >
<channel>
<title>javascript Tutorials </title>
<link>http://blog.111cn.net/zhongmao/category/29515.asp Tutorial X</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.111cn.net/zhongmao/archive/2004/09/15/105385.aspx</link>
<pubdate>wed, Sep 13:32:00 gmt</pubdate>
<guid>http://blog.111cn.net/zhongmao/archive/2004/09/15/105385.aspx</guid>
<comment>http://blog.111cn.net/zhongmao/comments/105385.aspx</comment>
<comments>http://blog.111cn.net/zhongmao/archive/2004/09/15/105385.aspx#feedback</comments>
<comments>2</comments>
<commentrss>http://blog.111cn.net/zhongmao/comments/commentrss/105385.aspx</commentrss>
<ping>http://blog.111cn.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.111cn.net/zhongmao/archive/2004/08/06/67161.aspx</link>
<pubdate>fri, Aug 16:33:00 gmt</pubdate>
<guid>http://blog.111cn.net/zhongmao/archive/2004/08/06/67161.aspx</guid>
<comment>http://blog.111cn.net/zhongmao/comments/67161.aspx</comment>
<comments>http://blog.111cn.net/zhongmao/archive/2004/08/06/67161.aspx#feedback</comments>
<comments>0</comments>
<commentrss>http://blog.111cn.net/zhongmao/comments/commentrss/67161.aspx</commentrss>
<ping>http://blog.111cn.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.111cn.net/zhongmao/archive/2004/08/02/58417.aspx</link>
<pubdate>mon, Aug 10:11:00 gmt</pubdate>
<guid>http://blog.111cn.net/zhongmao/archive/2004/08/02/58417.aspx</guid>
<comment>http://blog.111cn.net/zhongmao/comments/58417.aspx</comment>
<comments>http://blog.111cn.net/zhongmao/archive/2004/08/02/58417.aspx#feedback</comments>
<comments>0</comments>
<commentrss>http://blog.111cn.net/zhongmao/comments/commentrss/58417.aspx</commentrss>
<ping>http://blog.111cn.net/zhongmao/services/trackbacks/58417.aspx</ping>
<description>xmlhttpaaa ASD bb cc dd</description>
</item>
</channel>
</rss>
?

First, you create a DOMDocument object
$dom = new DOMDocument ();
Then load the XML file
$dom-> Load ("Test.xml");

Output XML file
Header ("content-type:text/xml;charset=gb2312");
echo $dom-> savexml ();

Save an XML file with a return value of int (file size, in bytes)
$dom-> Save ("Newfile.xml");

echo "$titles = $dom-> getElementsByTagName ("title");
foreach ($titles as $node) {
echo $node-> textcontent. "<br/>";
That's OK.
Echo $node->firstchild->data. "<br/>";
}

/*
echo "foreach ($dom->documentelement->childnodes as $items) {
If the node is an element (NodeType = 1) and the name is item, continue the loop
if ($items->nodetype = = 1 && $items->nodename = = "Item") {
foreach ($items->childnodes as $titles) {
If the node is an element, and the name is title, print it.
if ($titles->nodetype = = 1 && $titles->nodename = = "title") {
Print $titles->textcontent. "N";
}
}
}
}
*/

Querying data Using XPath
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
A bit deeper may be the case:
/rss/channel/item[position () = 1]/title Returns all of the first item element
/rss/channel/item/title[@id = ' 23 '] returns all title with an id attribute and a value of 23
/rss/channel/&folder&/title returns the title of all articles elements (the translator:&folder& represents the directory depth)
*/


Writing 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);

Removing nodes from the DOM
$dom->documentelement->removechild ($dom->documentelement->getelementsbytagname ("channel")-> Item (0));
Or use XPath to query out the node and delete it
$dom->documentelement->removechild ($xpath->query ("/rss/channel")->item (0));
$dom->save ("Newfile.xml");

Modifying node data from the DOM
Modify the first title file
This place is rather stupid, create a new node and replace the old one. If any friend has any other good ways, please tell me.
$firstTitle = $xpath->query ("/rss/channel/item/title")->item (0);
$newTitle = $dom->createelement ("title");
$newTitle->appendchild ("This's the new title text!!!")) (new Domtext);
$firstTitle->parentnode->replacechild ($newTitle, $firstTitle);
modifying properties
$firstTitle = $xpath->query ("/rss/channel/item/title")->item (0);
$firstTitle->setattribute ("by", "4");
$dom->save ("Newfile.xml");

echo "

//The following code obtains and parses the first page of the 111cn.net and returns the contents of the title element.
/*
$dom->loadhtmlfile ("http://www.111cn.net/");
$title = $dom->getelementsbytagname ("title");
Print $title->item (0)->textcontent;
*/

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.