DOMDocument方法解析xml节点

前端之家收集整理的这篇文章主要介绍了DOMDocument方法解析xml节点前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


可用PHP解析,也可用js解析。PHP和js直接的区别是,一个是用 . ,一个是 -> ,其他都一样

PHP中loadXML方法中参数是xml的字符串,load方法中参数是xml文件路径

  1. $dom = new DOMDocument();
  2. $dom->loadXML($xmlstring);
  3. $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</>
  3. headbodyname="bodyName"divid="id"name="hello">span>aaaa>bbbb>ccccdivbodymce:scripttype="text/ahref="http://lib.csdn.net/base/javascript"class='replace_word'title="JavaScript知识库"target='_blank'style='color:#df3434;font-weight:bold;'>JavaScripta>"><!--
  4. document.write("-----------------------节点属性-------------------------br>");
  5. varmyTest=document.getElementById("id");
  6. document.write(myTest.nodeType+" document.write(myTest.nodeName+">");
  7. document.write(myTest.nodeValue+" document.write("-----------------------节点attribute用法----------------------- document.write(myTest.getAttributeNode("name").nodeName+" document.write(myTest.getAttributeNode("name").nodeValue+" document.write(myTest.attributes("name").name+" document.write(myTest.attributes("name").value+" document.write("-----------------------firstChild用法------------------------- document.write(myTest.firstChild.nodeType+" document.write(myTest.firstChild.nodeName+" document.write(myTest.firstChild.nodeValue+" document.write("-----------------------lastChold用法------------------------- document.write(myTest.lastChild.nodeType+" document.write(myTest.lastChild.nodeName+" document.write(myTest.lastChild.nodeValue+" document.write("-----------------------childNodes用法------------------------- varnodes=myTest.childNodes;
  8. for(vari=0;inodes.length;i++){
  9. document.write(nodes[i].nodeName+"======"+nodes[i].innerHTML+" }
  10. document.write("-----------------------parentNode用法------------------------- varnodeP=myTest.parentNode;
  11. document.write(nodeP.nodeType+" document.write(nodeP.nodeName+" document.write(nodeP.nodeValue+" document.write(nodeP.getAttributeNode("name").nodeValue+" document.write("-----------------------hasChildNodes()用法------------------------- document.write(myTest.hasChildNodes());
  12. document.write(">-----------------------removeChild()用法------------------------- myTest.removeChild(myTest.firstChild);
  13. >-----------------------appendChild()用法------------------------- varnewd=document.createElement("span");
  14. newd.innerHTML="eeee";
  15. myTest.appendChild(newd);
  16. >-----------------------replaceChild()用法------------------------- myTest.replaceChild(newd,myTest.firstChild);
  17. >-----------------------insertBefore()用法------------------------- myTest.insertBefore(newd,myTest.lastChild);
  18. >-----------------------cloneNode()用法------------------------- varih=myTest.firstChild.cloneNode(true).innerHTML;
  19. document.getElementById("id").innerHTML=ih;
  20. //--mce:script>

PHP解析xml:PHP用DOMDocument解析xml,不能解析含有命名空间的xml,而且对节点的值有点问题,它包含子节点的值

xml:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <Meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>页面编辑</title>
  6. <link rel="stylesheet" type="text/css" href="../jquery-easyui-1.4.5/themes/default/easyui.css"/>
  7. <link rel="stylesheet" type="text/css" href="../jquery-easyui-1.4.5/themes/icon.css"/>
  8. <link rel="stylesheet" type="text/css" href="../jquery-easyui-1.4.5/demo/demo.css"/>
  9. <link rel="stylesheet" type="text/css" href="../css/index.css"/>
  10. <link rel="stylesheet" type="text/css" href="../css/easyui-change.css"/>
  11. <script type="text/javascript" src="../jquery-easyui-1.4.5/jquery.min.js"></script>
  12. <script type="text/javascript" src="../jquery-easyui-1.4.5/jquery.easyui.min.js"></script>
  13. <script type="text/javascript" src="../jsPackage/datagrid-groupview.js"></script>
  14. <script type="text/javascript" src="../jsPackage/datagrid-cellediting.js"></script>
  15. <script type="text/javascript" src="../JS/init/html_init.js"></script>
  16. <script type="text/javascript" src="../JS/topPanel/file_operations.js"></script>
  17. <script type="text/javascript" src="../JS/topPanel/edit_operations.js"></script>
  18. <script type="text/javascript" src="../JS/multipleUse/module_load.js"></script>
  19. <script type="text/javascript" src="../JS/rightPanel/output_tree_data.js"></script>
  20. </head>
  21. <body>
  22. <div id="topPanel" class="topPanel">
  23. <div class="easyui-panel" style="padding:5px;">
  24. <a href="#" class="easyui-menubutton" data-options="menu:'#mm0'">文件</a>
  25. <a href="#" class="easyui-menubutton" data-options="menu:'#mm1',iconCls:'icon-edit'">编辑</a>
  26. </div>
  27. <div id="mm0" style="width:150px;">
  28. <div id="menubutton_file_add" data-options="iconCls:'icon-add'">新建</div>
  29. <div class="menu-sep"></div>
  30. <div id="menubutton_file_open" data-options="iconCls:'icon-redo'">打开</div>
  31. <div class="menu-sep"></div>
  32. <div id="menubutton_file_save" data-options="iconCls:'icon-save'">存储</div>
  33. <div class="menu-sep"></div>
  34. <div id="menubutton_file_saveIN" data-options="iconCls:'icon-undo'">存储为</div>
  35. <div id="menubutton_file_out" class="menu-sep"></div>
  36. <div data-options="">退出</div>
  37. </div>
  38. <div id="mm1" style="width:150px;">
  39. <div>
  40. <span>插入JS脚本</span>
  41. <div>
  42. <div id="menubutton_edit_elementBefore">元素之前</div>
  43. <div class="menu-sep"></div>
  44. <div id="menubutton_edit_elementAfter">元素之后</div>
  45. </div>
  46. </div>
  47. <div class="menu-sep"></div>
  48. <div id="menubutton_edit_style">样式</div>
  49. <div class="menu-sep"></div>
  50. <div id="menubutton_edit_debug" data-options="iconCls:'icon-tip'">调试</div>
  51. </div>
  52. </div>
  53. <div id="leftPanel" class="leftPanel"></div>
  54. <div id="centerPanel" class="centerPanel">
  55. <!--
  56. <iframe id="centerShow" src="../show/show.html" style="width:100%;height:100%;overflow:auto;"></iframe>
  57. -->
  58. <div style="margin-top:50px;margin-left:50px;"><font size="4" color="blue">欢迎进入web页面编辑器</font></div>
  59. </div>
  60. <div id="rightPanel" class="rightPanel">
  61. <div id="edit" style="display:none">
  62. <div>
  63. <ul id="rightTreePanel" class="rightTreePanel"></ul>
  64. <div id="rightTree_menu"></div>
  65. </div>
  66. <div>
  67. <div id="rightDataGridPanel" class="rightDataGridPanel"></div>
  68. <div style="margin-top:2px">
  69. <input type="text" name="searchProperties" id="searchProperties"/>
  70. <font>自动刷新</font>
  71. <div id="autoRefresh_switchBtn"></div>
  72. <span id="handRefresh"></span>
  73. </div>
  74. </div>
  75. </div>
  76. </div>
  77. <div id="bottomPanel" class="bottomPanel">
  78. <div id="bottomPanel_panel" class="easyui-panel" style="padding:5px;height:50px;overflow-y:auto;"></div>
  79. </div>
  80. <div id="win"></div>
  81. <div id="style_window">
  82. </div>
  83. </body>
  84. </html>

PHP代码
  1. <?PHP
  2.  
  3. $myfile = fopen("comp.xml","r") or die("Unable to open file!");
  4. $xmlstring=fread($myfile,filesize("comp.xml"));
  5. fclose($myfile);
  6.  
  7.  
  8. $dom = new DOMDocument();
  9. $dom->loadXML($xmlstring);
  10.  
  11. $ary = array();
  12. $num = 0;
  13. $flag = "①";
  14. genArray($dom->documentElement,$ary,$num,$flag);
  15. print_r(json_encode($ary));
  16.  
  17. function genArray($node,&$ary,&$num,$flag){
  18. $nodeArray = array();
  19. $num = $num + 1;
  20. $numStr = $flag.$num;
  21. if ($node->hasAttributes()) {
  22. foreach ($node->attributes as $attr) {
  23. $nodeArray[$attr->nodeName] = $attr->nodeValue;
  24. }
  25. }
  26. if(isset($node->nodeValue)){
  27. echo "nodepath:".($node->getNodePath())."<br/>";
  28. $nodeArray['textValue'] = trim($node->nodeValue);
  29. }
  30.  
  31. if ($node->hasChildNodes()) {
  32. if ($node->childNodes->length == 1) {
  33. genArray($node->firstChild,$nodeArray,$flag);
  34. } else {
  35. foreach ($node->childNodes as $childNode) {
  36. if ($childNode->nodeType != XML_TEXT_NODE) {
  37. genArray($childNode,$flag);
  38. }
  39. }
  40. }
  41. }
  42. $ary[($node->nodeName).$numStr] = $nodeArray;
  43. }
  44. ?>


PHP代码中node同样也可以使用getElementsByTag("节点名称")来获取对应的节点。
  1. $childNode = $node->getElementsByTag("div").item(0);
  1.  
PHP和js直接的区别是,js中直接用document相当于PHP中的$PHPDocument
  1. $dom = new DOMDocument();
  2. $dom->loadXML($xmlstring);
  3. $PHPDocument = $dom->documentElement;

猜你在找的XML相关文章