DOMDocument方法解析xml節點
阿新 • • 發佈:2019-02-04
可用php解析,也可用js解析。php和js直接的區別是,一個是用 . ,一個是 -> ,其他都一樣
php中loadXML方法中引數是xml的字串,load方法中引數是xml檔案路徑
$dom = new DOMDocument();
$dom->loadXML($xmlstring);
$rootNode = $dom->documentElement;
node屬性
nodeType:顯示節點的型別
nodeName:顯示節點的名稱
nodeValue:顯示節點的值
attributes:獲取一個屬性節點
firstChild:表示某一節點的第一個節點
lastChild:表示某一節點的最後一個子節點
childNodes:表示所在節點的所有子節點
parentNode:表示所在節點的父節點
nextSibling:緊挨著當前節點的下一個節點
previousSibling:緊挨著當前節點的上一個節點
node方法
asChildNodes()方法:判定一個節點是否有子節點,有返回true,沒有返回false
removeChild()方法:去除一個節點
appendChild()方法:新增一個節點,如果文件樹中已經存在該節點,則將它刪除,然後在新位置插入
replaceChild()方法:從文件樹中刪除(並返回)指定的子節點,用另一個節點來替換它
insertBefore()方法:在指定節點的前面插入一個節點,如果已經存在,則刪除原來的,然後在新位置插入
cloneNode()方法:複製一個節點,該方法有一個引數,true表示同時複製所有的子節點,false表示近複製當前節點\
下面是js解析Dom
- <html>
- <title>node</title>
- <head>
- </head>
- <bodyname="bodyName">
- <divid = "id"name="hello"><span>aaaa</span><span>bbbb</span><span>cccc</span></div>
- </body>
- </html>
-
<mce:script
- document.write("-----------------------節點屬性-------------------------<br>");
-
var myTest = document
- document.write(myTest.nodeType+"<br>");
- document.write(myTest.nodeName+"<br>");
- document.write(myTest.nodeValue+"<br>");
- document.write("-----------------------節點attribute用法-----------------------<br>");
- document.write(myTest.getAttributeNode("name").nodeName+"<br>");
- document.write(myTest.getAttributeNode("name").nodeValue+"<br>");
- document.write(myTest.attributes("name").name+"<br>");
- document.write(myTest.attributes("name").value+"<br>");
- document.write("-----------------------firstChild用法-------------------------<br>");
- document.write(myTest.firstChild.nodeType+"<br>");
- document.write(myTest.firstChild.nodeName+"<br>");
- document.write(myTest.firstChild.nodeValue+"<br>");
- document.write("-----------------------lastChold用法-------------------------<br>");
- document.write(myTest.lastChild.nodeType+"<br>");
- document.write(myTest.lastChild.nodeName+"<br>");
- document.write(myTest.lastChild.nodeValue+"<br>");
- document.write("-----------------------childNodes用法-------------------------<br>");
- var nodes = myTest.childNodes;
- for(var i=0;i<nodes.length;i++){
- document.write(nodes[i].nodeName+"======"+nodes[i].innerHTML+"<br>");
- }
- document.write("-----------------------parentNode用法-------------------------<br>");
- var nodeP = myTest.parentNode;
- document.write(nodeP.nodeType+"<br>");
- document.write(nodeP.nodeName+"<br>");
- document.write(nodeP.nodeValue+"<br>");
- document.write(nodeP.getAttributeNode("name").nodeValue+"<br>");
- document.write("-----------------------hasChildNodes()用法-------------------------<br>");
- document.write(myTest.hasChildNodes());
- document.write("<br>-----------------------removeChild()用法-------------------------<br>");
- myTest.removeChild(myTest.firstChild);
- document.write("<br>-----------------------appendChild()用法-------------------------<br>");
- var newd = document.createElement("span");
- newd.innerHTML = "eeee";
- myTest.appendChild(newd);
- document.write("<br>-----------------------replaceChild()用法-------------------------<br>");
- myTest.replaceChild(newd,myTest.firstChild);
- document.write("<br>-----------------------insertBefore()用法-------------------------<br>");
- myTest.insertBefore(newd,myTest.lastChild);
- document.write("<br>-----------------------cloneNode()用法-------------------------<br>");
- var ih = myTest.firstChild.cloneNode(true).innerHTML;
- document.getElementById("id").innerHTML = ih;
- // --></mce:script>
php解析xml:php用DOMDocument解析xml,不能解析含有名稱空間的xml,而且對節點的值有點問題,它包含子節點的值
xml:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>頁面編輯</title>
<link rel="stylesheet" type="text/css" href="../jquery-easyui-1.4.5/themes/default/easyui.css"/>
<link rel="stylesheet" type="text/css" href="../jquery-easyui-1.4.5/themes/icon.css"/>
<link rel="stylesheet" type="text/css" href="../jquery-easyui-1.4.5/demo/demo.css"/>
<link rel="stylesheet" type="text/css" href="../css/index.css"/>
<link rel="stylesheet" type="text/css" href="../css/easyui-change.css"/>
<script type="text/javascript" src="../jquery-easyui-1.4.5/jquery.min.js"></script>
<script type="text/javascript" src="../jquery-easyui-1.4.5/jquery.easyui.min.js"></script>
<script type="text/javascript" src="../jsPackage/datagrid-groupview.js"></script>
<script type="text/javascript" src="../jsPackage/datagrid-cellediting.js"></script>
<script type="text/javascript" src="../JS/init/html_init.js"></script>
<script type="text/javascript" src="../JS/topPanel/file_operations.js"></script>
<script type="text/javascript" src="../JS/topPanel/edit_operations.js"></script>
<script type="text/javascript" src="../JS/multipleUse/module_load.js"></script>
<script type="text/javascript" src="../JS/rightPanel/output_tree_data.js"></script>
</head>
<body>
<div id="topPanel" class="topPanel">
<div class="easyui-panel" style="padding:5px;">
<a href="#" class="easyui-menubutton" data-options="menu:'#mm0'">檔案</a>
<a href="#" class="easyui-menubutton" data-options="menu:'#mm1',iconCls:'icon-edit'">編輯</a>
</div>
<div id="mm0" style="width:150px;">
<div id="menubutton_file_add" data-options="iconCls:'icon-add'">新建</div>
<div class="menu-sep"></div>
<div id="menubutton_file_open" data-options="iconCls:'icon-redo'">開啟</div>
<div class="menu-sep"></div>
<div id="menubutton_file_save" data-options="iconCls:'icon-save'">儲存</div>
<div class="menu-sep"></div>
<div id="menubutton_file_saveIN" data-options="iconCls:'icon-undo'">儲存為</div>
<div id="menubutton_file_out" class="menu-sep"></div>
<div data-options="">退出</div>
</div>
<div id="mm1" style="width:150px;">
<div>
<span>插入JS指令碼</span>
<div>
<div id="menubutton_edit_elementBefore">元素之前</div>
<div class="menu-sep"></div>
<div id="menubutton_edit_elementAfter">元素之後</div>
</div>
</div>
<div class="menu-sep"></div>
<div id="menubutton_edit_style">樣式</div>
<div class="menu-sep"></div>
<div id="menubutton_edit_debug" data-options="iconCls:'icon-tip'">除錯</div>
</div>
</div>
<div id="leftPanel" class="leftPanel"></div>
<div id="centerPanel" class="centerPanel">
<!--
<iframe id="centerShow" src="../show/show.html" style="width:100%;height:100%;overflow:auto;"></iframe>
-->
<div style="margin-top:50px;margin-left:50px;"><font size="4" color="blue">歡迎進入web頁面編輯器</font></div>
</div>
<div id="rightPanel" class="rightPanel">
<div id="edit" style="display:none">
<div>
<ul id="rightTreePanel" class="rightTreePanel"></ul>
<div id="rightTree_menu"></div>
</div>
<div>
<div id="rightDataGridPanel" class="rightDataGridPanel"></div>
<div style="margin-top:2px">
<input type="text" name="searchProperties" id="searchProperties"/>
<font>自動重新整理</font>
<div id="autoRefresh_switchBtn"></div>
<span id="handRefresh"></span>
</div>
</div>
</div>
</div>
<div id="bottomPanel" class="bottomPanel">
<div id="bottomPanel_panel" class="easyui-panel" style="padding:5px;height:50px;overflow-y:auto;"></div>
</div>
<div id="win"></div>
<div id="style_window">
</div>
</body>
</html>
php程式碼:
<?php
$myfile = fopen("comp.xml", "r") or die("Unable to open file!");
$xmlstring=fread($myfile,filesize("comp.xml"));
fclose($myfile);
$dom = new DOMDocument();
$dom->loadXML($xmlstring);
$ary = array();
$num = 0;
$flag = "①";
genArray($dom->documentElement, $ary, $num, $flag);
print_r(json_encode($ary));
function genArray($node, &$ary ,&$num, $flag){
$nodeArray = array();
$num = $num + 1;
$numStr = $flag.$num;
if ($node->hasAttributes()) {
foreach ($node->attributes as $attr) {
$nodeArray[$attr->nodeName] = $attr->nodeValue;
}
}
if(isset($node->nodeValue)){
echo "nodepath:".($node->getNodePath())."<br/>";
$nodeArray['textValue'] = trim($node->nodeValue);
}
if ($node->hasChildNodes()) {
if ($node->childNodes->length == 1) {
genArray($node->firstChild, $nodeArray,$num, $flag);
} else {
foreach ($node->childNodes as $childNode) {
if ($childNode->nodeType != XML_TEXT_NODE) {
genArray($childNode, $nodeArray,$num, $flag);
}
}
}
}
$ary[($node->nodeName).$numStr] = $nodeArray;
}
?>
在php程式碼中node同樣也可以使用getElementsByTag("節點名稱")來獲取對應的節點。
$childNode = $node->getElementsByTag("div").item(0);
php和js直接的區別是,js中直接用document相當於php中的$phpDocument
$dom = new DOMDocument();
$dom->loadXML($xmlstring);
$phpDocument = $dom->documentElement;