DOMDocument方法解析xml节点

时间:2022-10-20 19:32:14


可用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

[xhtml] view plain copy
  1. <html>  
  2.     <title>node</title>  
  3.     <head>  
  4.     </head>  
  5. <body name="bodyName">  
  6.     <div id = "id" name="hello"><span>aaaa</span><span>bbbb</span><span>cccc</span></div>  
  7. </body>  
  8. </html>  
  9. <mce:script type="text/<a href="http://lib.csdn.net/base/javascript" class='replace_word' title="JavaScript知识库" target='_blank' style='color:#df3434; font-weight:bold;'>JavaScript</a>"><!--  
  10.     document.write("-----------------------节点属性-------------------------<br>");  
  11.     var myTest = document.getElementById("id");  
  12.     document.write(myTest.nodeType+"<br>");  
  13.     document.write(myTest.nodeName+"<br>");  
  14.     document.write(myTest.nodeValue+"<br>");  
  15.     document.write("-----------------------节点attribute用法-----------------------<br>");  
  16.     document.write(myTest.getAttributeNode("name").nodeName+"<br>");  
  17.     document.write(myTest.getAttributeNode("name").nodeValue+"<br>");  
  18.     document.write(myTest.attributes("name").name+"<br>");  
  19.     document.write(myTest.attributes("name").value+"<br>");  
  20.     document.write("-----------------------firstChild用法-------------------------<br>");  
  21.     document.write(myTest.firstChild.nodeType+"<br>");  
  22.     document.write(myTest.firstChild.nodeName+"<br>");  
  23.     document.write(myTest.firstChild.nodeValue+"<br>");  
  24.     document.write("-----------------------lastChold用法-------------------------<br>");  
  25.     document.write(myTest.lastChild.nodeType+"<br>");  
  26.     document.write(myTest.lastChild.nodeName+"<br>");  
  27.     document.write(myTest.lastChild.nodeValue+"<br>");  
  28.     document.write("-----------------------childNodes用法-------------------------<br>");  
  29.     var nodes = myTest.childNodes;  
  30.     for(var i=0;i<nodes.length;i++){  
  31.         document.write(nodes[i].nodeName+"======"+nodes[i].innerHTML+"<br>");  
  32.     }  
  33.     document.write("-----------------------parentNode用法-------------------------<br>");  
  34.     var nodeP = myTest.parentNode;  
  35.     document.write(nodeP.nodeType+"<br>");  
  36.     document.write(nodeP.nodeName+"<br>");  
  37.     document.write(nodeP.nodeValue+"<br>");  
  38.     document.write(nodeP.getAttributeNode("name").nodeValue+"<br>");  
  39.     document.write("-----------------------hasChildNodes()用法-------------------------<br>");  
  40.     document.write(myTest.hasChildNodes());  
  41.     document.write("<br>-----------------------removeChild()用法-------------------------<br>");  
  42.     myTest.removeChild(myTest.firstChild);  
  43.     document.write("<br>-----------------------appendChild()用法-------------------------<br>");  
  44.     var newd = document.createElement("span");  
  45.     newd.innerHTML = "eeee";  
  46.     myTest.appendChild(newd);  
  47.     document.write("<br>-----------------------replaceChild()用法-------------------------<br>");  
  48.     myTest.replaceChild(newd,myTest.firstChild);  
  49.     document.write("<br>-----------------------insertBefore()用法-------------------------<br>");  
  50.     myTest.insertBefore(newd,myTest.lastChild);  
  51.     document.write("<br>-----------------------cloneNode()用法-------------------------<br>");  
  52.     var ih = myTest.firstChild.cloneNode(true).innerHTML;  
  53.     document.getElementById("id").innerHTML = ih;  
  54. // --></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;