Home > Backend Development > XML/RSS Tutorial > Implementation code for parsing php DOMElement to operate xml documents

Implementation code for parsing php DOMElement to operate xml documents

高洛峰
Release: 2016-12-24 11:35:20
Original
1726 people have browsed it

/*<?xml version="1.0" encoding="UTF-8" standalone="no"?>
 <!-- css的样式定义,不加点。如:name{color:red;} -->
<?xml-stylesheet type="text/css" href="css.css"?>
<!-- 引入dtd文档定义文件 (根元素:班级)<!DOCTYPE 班级 SYSTEM "class.dtd" /> -->
<!-- <!DOCTYPE 班级[
<!ELEMENT 班级 (学生+)>
<!ELEMENT 学生 (名字,年龄,介绍)>
<!ELEMENT 名字 (#PCDATA)>
<!ELEMENT 年龄 (#PCDATA)>
<!ELEMENT 介绍 (#PCDATA)>
] /> -->
<班级>
<学生 number="101">
<名字>孙悟空</名字>
<名字>孙行者</名字>
<年龄>123</年龄>
<介绍><![CDATA[&*$%特殊字串^&#$&]]></介绍>
</学生>
<学生 number="10"2">
<名字>白骨精</名字>
<年龄>140</年龄>
<介绍>介绍内容</介绍>
</学生>
</班级>
*/
$xmldoc = new DOMDocument(&#39;1.0&#39;, &#39;UTF-8&#39;);
$xmldoc->load(&#39;datas.xml&#39;);
$itemsNodeList = $xmldoc->getElementsbyTagName(&#39;学生&#39;);
$itemElement = $itemsNodeList->item(0);//得到第一个完整的学生信息节点
$itemChildsNodeList = $itemElement->getElementsbyTagName(&#39;名字&#39;);//得到子节点“名字”,也许有多个名字
$itemChildNode = $itemChildsNodeList->item(0);//得到第一个名字节点
echo $itemChildNode->nodeValue;//输出节点值
//封装成函数
$nodeArr = array(&#39;名字&#39;, &#39;年龄&#39;, &#39;介绍&#39;); 
function getNodeVal($xmldoc, $itemsName, $nodeArr){
    $items = $xmldoc->getElementsByTagName($itemsName);
     for($i=0; $i < $items->length; $i++){
        $item = $items->item($i);
        foreach($nodeArr as $node){
            $data[$i][] = $item->getElementsByTagName($node)->item(0)->nodeValue;
        }
    } 
    return $data;
}
$data = getNodeVal($xmldoc, &#39;学生&#39;, $nodeArr);
print_r($data);
Copy after login
//添加节点
$xmldoc = new DOMDocument(&#39;1.0&#39;, &#39;UTF-8&#39;);
$xmldoc->load(&#39;datas.xml&#39;);
$items = $xmldoc->getElementsByTagName(&#39;班级&#39;)->item(0);//根节点
$student =  $xmldoc->createElement(&#39;学生&#39;);//创建一个新的学生节点
$stu_name = $xmldoc->createElement(&#39;名字&#39;,&#39;张三&#39;);
$stu_age = $xmldoc->createElement(&#39;年龄&#39;,&#39;15&#39;);
$stu_intro = $xmldoc->createElement(&#39;介绍&#39;,&#39;动手能力强且成绩稳定&#39;);
$items->appendChild($student);
$student->appendChild($stu_name);
$student->appendChild($stu_age);
$student->appendChild($stu_intro);
$bytes = $xmldoc->save(&#39;datas.xml&#39;); 
echo ($bytes)? "写入了: $bytes 字节" : &#39;保存失败&#39;;
//删除节点
$xmldoc = new DOMDocument(&#39;1.0&#39;, &#39;UTF-8&#39;);
$xmldoc->load(&#39;datas.xml&#39;);
$student = $xmldoc->getElementsByTagName(&#39;学生&#39;)->item(2);//直接找到要删除的节点
$student->parentNode->removeChild($student);//父节点的删除方法
$xmldoc->save(&#39;datas.xml&#39;);
//修改节点值
$student = $xmldoc->getElementsByTagName(&#39;学生&#39;)->item(2);
$student->getElementsByTagName(&#39;年龄&#39;)->item(0)->nodeValue += 10;
$student->setAttribute(&#39;id&#39;, &#39;110&#39;);
$xmldoc->save(&#39;datas.xml&#39;);
//应用 Xpath 查找节点
$xml = new DOMDocument(&#39;1.0&#39;, &#39;UTF-8&#39;);
$xml->load(&#39;dat.xml&#39;);
$xpath = new DOMXPath($xml);
$nodeList = $xpath->query(&#39;/aaa/bbb/ddd/fff&#39;);
echo $nodeList->item(0)->nodeValue;
//SimpleXML 类操作 xml
/*
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book house="清华出版">
<code>1001</code>
<price>200元</price>
<author>大明</author>
<title>天龙八部</title>
</book>
<book house="北大出版">
<code>1002</code>
<price>321元</price>
<author>张三</author>
<title>笑傲江湖</title>
</book>
<book house="人 民出版">
<code>1004</code>
<price>182元</price>
<author>李四</author>
<title>读者</title>
</book>
</books>
*/
$xml = simplexml_load_file(&#39;books.xml&#39;);
$books = $xml->book;
echo $books[1]->title . $books[1][&#39;house&#39;];//直接指向第二本书
foreach($xml as $item){
    echo $item->title,&#39; &#39;,$item[&#39;house&#39;],&#39;<br/>&#39;;
}
Copy after login


For more analysis on the implementation code of php DOMElement operation xml document, please pay attention to the PHP Chinese website for related articles!


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