Home > Backend Development > PHP Tutorial > Using DOM to control XML_PHP tutorial in PHP5

Using DOM to control XML_PHP tutorial in PHP5

WBOY
Release: 2016-07-13 17:32:04
Original
1131 people have browsed it

php(as the current mainstream development language)5 has enhanced support for xml(standardization is getting closer), and uses DOM to extend xml( Standardization is getting closer)Ability to operate. These functions are part of the core of php(as the current mainstream development language)5 and can be used without installation.

The following example simply demonstrates the operation of DOM on xml(standardization is getting closer). For detailed explanation, please see the comments in the code

/**************************************************
* * use xml(standardization is getting closer) in php(as the current mainstream development language)5
** reference site:
** http: //cn.php(as the current mainstream development language).net/manual/zh/ref.dom.php(as the current mainstream development language)
** the follow codes need php(as the current mainstream development language)5 support
************************ **************************/


//First create a DOMDocument object
$dom = new DomDocument();
//Then load xml(Standardization is getting closer)File
$dom -> load("test.xml(Standardization is getting closer)");

/ /Output xml(Standardization is getting closer)File
//header("Content-type: text/xml(Standardization is getting closer);charset= gb2312");
//echo $dom -> savexml(Standardization is getting closer)();

//Save xml(Standardization is getting closer) Close) file, the return value is int (file size in bytes)
//$dom -> save("newfile.xml(Standardization is getting closer)");

echo "


Get all title elements:
";
$titles = $dom -> getElementsByTagName("title");
foreach ($titles as $node)
{
echo $node -> textContent . "
";
//This works too
//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 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. " ";
}
}
}
}
*/

//Query data using XPath

echo "
Query title using Node results:
";

$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
A deeper look may be like this:
/rss/channel/item[position() = 1]/title returns all the first item element
/rss/channel/ item/title[@id = 23] Returns all titles containing the id attribute and the value is 23
/rss/channel/&folder&/title Returns all titles under the articles element (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 node 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
(Standardization is getting closer)
");//Modify node data from DOM

//Modify the first title file

//This place is stupid, create a new node, and then replace the old one node. If anyone has other good methods, please tell me

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508732.htmlTechArticlephp (as the current mainstream development language) enhanced xml in 5 (standardization is getting closer) Support, using DOM to extend the capabilities of xml (standardization is getting closer) operations. These functions serve as...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template