PHP5 has enhanced XML support and uses DOM to expand XML operation capabilities. These functions are part of the PHP5 core and do not need to be installed to be used.
The following example simply demonstrates the operation of DOM on XML. For detailed explanation, please see the comments in the code
<?
/************************************************
** use XML in PHP5
** reference site:
** http://cn.php.net/manual/zh/ref.dom.php
** the follow codes need PHP5 support
** www.knowsky.com
*************************************************/
//First 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 the XML file, the return value is int (file size in bytes)
//$dom -> save("newfile.xml");
echo "<hr/>Get all title elements:<hr/>";
$titles = $dom -> getElementsByTagName("title");
foreach ($titles as $node)
{
echo $node -> textContent . "<br/>";
//This is also ok
//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 the name is item, continue looping
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";
}
}
}
}
*/
//Use XPath to query data
echo "<hr/>The title node result of using XPath query:<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 if Xpath is more powerful and in-depth, it may be like this:
/rss/channel/item[position() = 1]/title returns all elements of the first item element
/rss/channel/item/title[@id = '23'] Returns all titles that contain the id attribute and have a value of 23
/rss/channel/&folder&/title returns the titles under all articles elements (Translator’s Note: &folder& represents directory depth)
*/
//Write new data to 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 nodes from DOM
//$dom->documentElement->RemoveChild($dom->documentElement->getElementsByTagName("channel")->item(0));
//Or use xpath to query the node and delete it
//$dom->documentElement->RemoveChild($xpath->query("/rss/channel")->item(0));
//$dom->save("newfile.xml");
//Modify node data from DOM
//Modify the first title file
//This place is stupid, create a new node, and then replace the old node. If anyone has any 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 properties
//$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 obtains and parses php The homepage of .net will return the content of the first title element.
/*
$dom->loadHTMLFile(" http://www.php.net/ ");
$title = $dom->getElementsByTagName("title");
print $title->item(0)->textContent;
*/
?>
The following is the test.xml file code:
<?xml version="1.0" encoding="gb2312"?>
<rss version="2.0">
<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>