DOM is used in PHP5 to control the XML implementation code
- 2020-03-31 20:41:33
- OfStack
The following example is a simple example of how the DOM works with XML. See the comments in the code for more details
Here is the code for the test.xml file:
<?
/************************************************
** use XML in PHP5
** reference site:
** http://cn.php.net/manual/zh/ref.dom.php
** the follow codes need PHP5 support
*************************************************/
//The first step is to 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 and return an int (file size, in bytes)
//$dom -> save("newfile.xml");
echo "<hr/> Get all of title The element :<hr/>";
$titles = $dom -> getElementsByTagName("title");
foreach ($titles as $node){
echo $node -> textContent . "<br/>";
//That's ok
//echo $node->firstChild->data . "<br/>";
}
/*
echo "<hr/> Traversing all nodes from the root: <br/>";
foreach ($dom->documentElement->childNodes as $items) {
//If the node is an element (nodeType == 1) and the name is item, the loop continues
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 the data
echo "<hr/> use XPath Of the query title Results the node :<hr/>";
$xpath = new domxpath($dom);
$titles = $xpath->query("/rss/channel/item/title");
foreach ($titles as $node){
echo $node->textContent."<br/>";
}
/*
So and use getElementsByTagName() Same way, but Xpath Much stronger
A little deeper might look like this:
/rss/channel/item[position() = 1]/title Return to the first item All of the elements
/rss/channel/item/title[@id = '23'] Returns all contains id Property and the value is 23 the title
/rss/channel/&folder&/title Returns all articles subelement title( The translator's note: &folder& Represents directory depth )
*/
//Writes 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);
//Remove the node from the DOM
//$dom->documentElement->RemoveChild($dom->documentElement->getElementsByTagName("channel")->item(0));
//Or you can use xpath to query out the nodes and then delete them
//$dom->documentElement->RemoveChild($xpath->query("/rss/channel")->item(0));
//$dom->save("newfile.xml");
//Modify the node data from the DOM
//Modify the file for the first title
//This is a silly place to create a new node and replace the old one. If any friends have other good methods, please be sure to let me know
$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 the properties
//$firstTitle = $xpath->query("/rss/channel/item/title")->item(0);
//$firstTitle->setAttribute("orderby", "4");
$dom->save("newfile.xml");
echo "<hr/><a href="newfile.xml"> To view newfile.xml</a>";
//The following code gets and parses the home page of php.net, returning the contents of the first title element.
/*
$dom->loadHTMLFile("http://www.php.net/");
$title = $dom->getElementsByTagName("title");
print $title->item(0)->textContent;
*/
?>
Here is the code for the test.xml file:
<?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>