Using DOM to control XML in PHP5(1)

黄舟
Release: 2023-03-03 14:08:01
Original
1154 people have browsed it

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 the 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 "


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.
if ($titles->nodeType == 1 && $ titles->nodeName == "title") {
Print $titles->textContent . "n";
}
}
}
}
*/

//Use XPath to query data
echo "
The title node result of using XPath query:
";
$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 requires It’s much more powerful
A little deeper, it might be like this:
/rss/channel/item[position() = 1]/title Returns all
/rss/channel/item/title[@id = '23' of the first item element ] Return all titles that contain the id attribute and have a value of 23
/rss/channel/&folder&/title Return all titles under the articles element (Translator’s Note: &folder& represents directory depth)
*/

The above is using DOM in PHP5 Control the content of XML(1), For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!