ITeye博客

JavaScript解析XML文档成树菜单

dhj · 2008-09-27 14:18 · web页面技术 · Markdown

1.解析xml的BlueParseXML.js文件

  1. /**
  2. * @description 解析并封装XML
  3. * @author BluesLee
  4. * @lastModif BluesLee
  5. * @createDate 2007-10-13
  6. * @modifDate 2007-10-15
  7. * @version 1.0
  8. */
  9. //解析XML文件对象构造器
  10. function  BlueParseXML(){
  11. }
  12. BlueParseXML.prototype.xml; //定义对象属性xml
  13. BlueParseXML.prototype.loadFinish; //加载完成函数
  14. BlueParseXML.prototype.nodeProperties= new  Array(); //节点属性数组
  15. BlueParseXML.prototype.key= "key" ;
  16. BlueParseXML.prototype.value= "value" ;
  17. BlueParseXML.prototype.nodeList; //节点集合
  18. BlueParseXML.prototype.rootName;
  19. BlueParseXML.XML_NULL=-1; //未加载xml,或加载失败
  20. //加载xml文件
  21. BlueParseXML.prototype.loadXML= function (url){
  22. var  newXML;
  23. var  obj= this ;
  24. //如果它受支持,采用标准的2级DOM技术
  25. if (document.implementation && document.implementation.createDocument){
  26. //创建新的Document对象
  27. //第一个参数是包含文档所使用的命名空间URI的字符串
  28. //第二个参数是包含文档根元素名称的字符串
  29. //第三个参数是要创建的文档类型(也称为doctype)。
  30. //现在Firefox中并不提供针对文档类型的JavaScript支持,所以第三个参数总是为null。
  31. newXML=document.implementation.createDocument( "" , "" , null );
  32. //设置装载完毕时触发事件
  33. newXML.onload= function (){
  34. if (obj.loadFinish){
  35. obj.loadFinish();
  36. }
  37. }
  38. newXML.load(url);
  39. } else { //IE浏览器创建Document对象
  40. newXML= new  ActiveXObject( "Microsoft.XMLDOM" );
  41. //设置onload
  42. newXML.onreadystatechange= function (){
  43. if (newXML.readyState==4){
  44. if (obj.loadFinish){
  45. obj.loadFinish();
  46. }
  47. }
  48. }
  49. newXML.load(url);
  50. }
  51. this .xml=newXML;
  52. }
  53. //解析遍历xml
  54. BlueParseXML.prototype.parseXML= function (){
  55. if (! this .xml){
  56. alert( "XML未加载,请先加载XML文件!" );
  57. return  parseXML.XML_NULL;
  58. }
  59. var  rootNode= this .xml.documentElement;
  60. this .rootName=rootNode.tagName;
  61. this .nodeList= new  ChildNodeList();
  62. this .level= this .iteratorNode( this .nodeList,rootNode,1);
  63. return   this .level;
  64. }
  65. //遍历节点,将节点信息封装存入ChildNodeList中
  66. BlueParseXML.prototype.iteratorNode= function (list,node,level){
  67. if (!isNaN(level) && level>0){
  68. list.nodeLevel=level;
  69. }
  70. var  result=list.nodeLevel; //节点级别
  71. var  flag= true ; //判断节点是否有子节点标记
  72. for ( var  child=node.firstChild;child!= null ;child=child.nextSibling){
  73. if (document.implementation && document.implementation.createDocument && !child.getAttribute){
  74. continue ;
  75. }
  76. flag= false ; //有子节点
  77. if (!list.nodeName || list.nodeName== "" ){
  78. list.nodeName=child.tagName;
  79. }
  80. if (list.nodeName==child.tagName){
  81. var  nodeInfo= new  NodeInfo();
  82. nodeInfo.nodeLevel=list.nodeLevel; //设置节点级别
  83. nodeInfo.nodeName=list.nodeName; //设置节点名称
  84. nodeInfo.key=child.getAttribute( this .key); //设置节点属性
  85. nodeInfo.value=child.getAttribute( this .value);
  86. for ( var  index=0;index< this .nodeProperties.length;index++){
  87. nodeInfo[ this .nodeProperties[index]]=child.getAttribute( this .nodeProperties[index]);
  88. }
  89. list.addElement(nodeInfo.key,nodeInfo); //把节点加入父节点的子节点集合
  90. var  n= this .iteratorNode(nodeInfo.childNodeList,child,list.nodeLevel+1);
  91. if (n>result){ //如果子节点返回的节点级别大于改节点级别,设置返回节点级别为最大级别
  92. result=n;
  93. }
  94. }
  95. }
  96. if (flag){ //当改节点没有子节点时返回的节点级别-1
  97. result=result-1;
  98. }
  99. return  result;
  100. }
  101. //扩展String对象,增加trim方法去掉字符串前后空格
  102. String.prototype.trim= function (){
  103. return   this .replace(/^\s*/g, "" ).replace(/\s*$/g, "" );
  104. }
  105. /**
  106. * @description 自定义集合对象(利用对象属性键值对,类似Hashtable使用)
  107. * @author BluesLee
  108. * @lastModif BluesLee
  109. * @createDate 2007-10-13
  110. * @modifDate 2007-10-15
  111. * @version 1.0
  112. */
  113. ChildNodeList.prototype.nodeLevel=1;
  114. ChildNodeList.prototype.nodeName;
  115. ChildNodeList.prototype.keyArray;
  116. ChildNodeList.prototype.ELEMENT_NULL=-1; //元素为空
  117. ChildNodeList.prototype.ELEMENT_EXIST=-2; //元素已经存在
  118. ChildNodeList.prototype.ELEMENT_NOT_EXIST=-3; //元素不存在
  119. ChildNodeList.prototype.INDEX_OUT=-4 //数组下标越界
  120. ChildNodeList.prototype.ERROR_DELETE=-5 //删除元素出错
  121. function  ChildNodeList(){
  122. this .keyArray= new  Array();
  123. }
  124. //添加集合元素
  125. ChildNodeList.prototype.addElement= function (key,element){
  126. if (!key || !element){
  127. return  ChildNodeList.ELEMENT_NULL;
  128. }
  129. if ( this [key]){
  130. return  ChildNodeList.ELEMENT_EXIST;
  131. }
  132. var  index= this .keyArray.length;
  133. this .keyArray[index]=key;
  134. this [key]=element;
  135. return  index;
  136. }
  137. //根据下标删除集合元素
  138. ChildNodeList.prototype.deleteElementByIndex= function (index){
  139. if (index>= this .keyArray.length){
  140. return  ChildNodeList.INDEX_OUT;
  141. }
  142. var  key= this .keyArray[index];
  143. for ( var  i=index;i< this .keyArray.length;i++){
  144. this .keyArray[i]= this .keyArray[i+1];
  145. }
  146. this .keyArray.length= this .keyArray.length-1;
  147. delete   this [key];
  148. return  index;
  149. }
  150. //根据key删除集合元素
  151. ChildNodeList.prototype.deleteElementByKey= function (key){
  152. if (! this [key]){
  153. return  ChildNodeList.ELEMENT_NOT_EXIST;
  154. }
  155. for ( var  index=0;index< this .keyArray.length;index++){
  156. if ( this .keyArray[index]==key){
  157. return   this .deleteElementByIndex(index);
  158. }
  159. }
  160. return  ChildNodeList.ERROR_DELETE;
  161. }
  162. //根据key查找元素
  163. ChildNodeList.prototype.getElementByKey= function (key){
  164. return   this [key];
  165. }
  166. //根据下标查找元素
  167. ChildNodeList.prototype.getElementByIndex= function (index){
  168. return   this .getElementByKey( this .keyArray[index]);
  169. }
  170. //获取集合大小
  171. ChildNodeList.prototype.size= function (){
  172. return   this .keyArray.length;
  173. }
  174. /**
  175. * @description 节点信息封装对象
  176. * @author BluesLee
  177. * @lastModif BluesLee
  178. * @createDate 2007-10-13
  179. * @modifDate 2007-10-15
  180. * @version 1.0
  181. */
  182. NodeInfo.prototype.nodeLevel;
  183. NodeInfo.prototype.nodeName; //节点名称
  184. NodeInfo.prototype.key; //节点名称,对应xml节点key属性
  185. NodeInfo.prototype.value; //节点内容,对应xml节点value属性
  186. NodeInfo.prototype.childNodeList; //子节点列表,用ChildNodeList对象封装
  187. //构造器
  188. function  NodeInfo(){
  189. this .childNodeList= new  ChildNodeList();
  190. }

2.BlueTree.js

  1. /**
  2. * xml树对象 <br>
  3. * Author:BluesLee <br>
  4. * CreateDate:2008-7-9 <br>
  5. * Modifier:BluesLee <br>
  6. * ModifyDate:2008-7-9 <br>
  7. * Version:1.1<br>
  8. * Copyright(c)2008 深蓝工作室<br>
  9. * All right reserved.<br>
  10. *
  11. */
  12. BlueTree.prototype.nodeArrayList; //节点总集合
  13. BlueTree.prototype.isAddOnChangeEvent= false ;
  14. BlueTree.prototype.SELECTLIST_NULL=-1; //下拉对象数组为空
  15. BlueTree.prototype.SELECTLIST_TYPE_NOT_ARRAY=-2; //下拉对象数组不是数组对象
  16. BlueTree.prototype.SELECTLIST_EMPTY=-3; //下拉对象数组为空数组
  17. BlueTree.prototype.SELECTLIST_LENGTH_OUT=-4; //下拉对象数组小于节点集合级别
  18. BlueTree.prototype.NODELIST_EMPTY=-5; //节点集合为空
  19. BlueTree.prototype.EVENT_NULL=-6; //事件源为空
  20. //树属性
  21. BlueTree.prototype.rootId;  //根节点ID
  22. BlueTree.prototype.blueURL;  //点击节点超连接---isUseDataURL为false时有用
  23. BlueTree.prototype.isUseDataURL;  //是否使用节点封装对象的节点超连接
  24. BlueTree.prototype.rootName;  //根节点名称
  25. BlueTree.prototype.imageURL;  //所有图片目录路径
  26. BlueTree.prototype.rootImage;  //根节点图片
  27. BlueTree.prototype.folderImg;  //有子节点关闭图片
  28. BlueTree.prototype.folderOpenImg;  //有子节点展开图片
  29. BlueTree.prototype.fileImg;  //无子节点图片
  30. BlueTree.prototype.openImg;  //节点展开图片
  31. BlueTree.prototype.closeImg;  //节点关闭图片
  32. BlueTree.prototype.elementLineImg;  //带有节点的线图片
  33. BlueTree.prototype.lineImg;  //无节点的线图片
  34. BlueTree.prototype.endLineImg;  //最后一个节点线图片
  35. BlueTree.prototype.endFolderOpenImg;  //最后一个有子节点展开图片
  36. BlueTree.prototype.endFolderCloseImg;  //最后一个有子节点关闭图片
  37. BlueTree.prototype.noneImg; //空白图片
  38. BlueTree.prototype.isFolderURL;  //是否给有子节点的节点加超连接---isUseDataURL为false时有用
  39. BlueTree.prototype.rootURL;  //根节点连接
  40. BlueTree.prototype.target;  //所有连接的target属性
  41. BlueTree.prototype.linkClass;  //超连接样式
  42. BlueTree.prototype.styleClass;  //总样式
  43. BlueTree.prototype.imgClass;  //图片样式
  44. BlueTree.prototype.useDataImg;  //节点图片是否使用数据类图片,no为不使用,first为仅第一级目录使用,all全部使用
  45. BlueTree.prototype.onMouseOver;  //鼠标经过每个节点时的样式
  46. BlueTree.prototype.onMouseOut;  //鼠标离开每个节点时的样式
  47. BlueTree.prototype.isCheckBox;  //是否带复选框控件
  48. BlueTree.prototype.checkBoxName;  //复选框控件name
  49. //构造器
  50. function  BlueTree(nodeList){
  51. if (nodeList){
  52. this .nodeArrayList=nodeList;
  53. } else {
  54. this .nodeArrayList= new  ChildNodeList();
  55. }
  56. //初始化树属性
  57. this .rootId =  "0" ;
  58. this .blueURL =  "javascript:void(0);" ;
  59. this .isUseDataURL =  "false" ;
  60. this .rootName =  "根节点" ;
  61. this .rootImage =  "rootImage.gif" ;
  62. this .imageURL =  "images/" ;  //必须设置
  63. this .folderImg =  "folder.gif" ;
  64. this .folderOpenImg =  "folderOpen.gif" ;
  65. this .fileImg =  "file.gif" ;
  66. this .openImg =  "open.gif" ;
  67. this .closeImg =  "close.gif" ;
  68. this .elementLineImg =  "elementLine.gif" ;
  69. this .lineImg =  "line.gif" ;
  70. this .endLineImg =  "endLine.gif" ;
  71. this .endFolderOpenImg =  "endFolderOpen.gif" ;
  72. this .endFolderCloseImg =  "endFolderClose.gif" ;
  73. this .noneImg =  "none.gif" ;
  74. this .isFolderURL =  "true" ;
  75. this .rootURL =  "javascript:void(0);" ;
  76. this .target =  "_blank" ;
  77. this .linkClass =  "" ;
  78. this .imgClass =  "" ;
  79. this .styleClass =  "" ;
  80. this .useDataImg =  "no" ;
  81. this .onMouseOver =  "#CCCCCC" ;
  82. this .onMouseOut =  "" ;
  83. this .isCheckBox =  "true" ;
  84. this .checkBoxName =  "checkbox" ;
  85. }
  86. /**
  87. * 根据节点集合构建树 <br>
  88. * Author:李鹏 <br>
  89. * CreateDate:2008-9-19 <br>
  90. * Modifier:李鹏 <br>
  91. * ModifyDate:2008-9-19 <br>
  92. * Version:1.1<br>
  93. *
  94. * @param parentObject,树所隶属父节点
  95. * @param nodeList,即将展开节点集合
  96. */
  97. BlueTree.prototype.createTree= function (parentObject,nodeList){
  98. parentObject.innerHTML= "" ;
  99. if (!nodeList){
  100. nodeList= this .nodeArrayList;
  101. }
  102. var  level=1;
  103. var  tableNode=document.createElement( "table" );
  104. tableNode.className= this [ "styleClass" ];
  105. tableNode.style.cssText= "border-collapse:collapse;border:0px solid;padding:0px;margin:0px;" ;
  106. tableNode.border=0;
  107. tableNode.cellPadding=0;
  108. tableNode.cellSpacing=0;
  109. //创建第N级节点
  110. for ( var  i=0;i<nodeList.size();i++){
  111. var  nodeInfo=nodeList.getElementByIndex(i);
  112. var  n_id= "" ,name= "" ,url= "" ,ischild= "false" ,openimg= "" ,closeimg= "" ;
  113. if (nodeInfo.key){
  114. n_id=nodeInfo.key;
  115. }
  116. if (nodeInfo.value){
  117. name=nodeInfo.value;
  118. }
  119. if (nodeInfo.url){
  120. url=nodeInfo.url;
  121. }
  122. if (nodeInfo.childNodeList.size()>0){
  123. ischild= "true" ;
  124. }
  125. if (nodeInfo.openimg){
  126. openimg=nodeInfo.openimg;
  127. }
  128. if (nodeInfo.closeimg){
  129. closeimg=nodeInfo.closeimg;
  130. }
  131. if (nodeInfo.nodeLevel && nodeInfo.nodeLevel!=level){
  132. level=nodeInfo.nodeLevel;
  133. }
  134. //添加行
  135. var  row=tableNode.insertRow(tableNode.rows.length);
  136. addEvent(row, "mouseover" ,mouseOverNode);
  137. addEvent(row, "mouseout" ,mouseOutNode);
  138. //添加单元格
  139. var  cell=row.insertCell(row.length);
  140. cell.style.cssText= "white-space:nowrap;" ;
  141. //根据节点级别加递进线图片
  142. var  previousNodes;
  143. try {
  144. previousNodes=parentObject.parentNode.previousSibling.firstChild.childNodes;
  145. for ( var  j=1;j<level;j++){
  146. var  img=document.createElement( "img" );
  147. img.setAttribute( "align" , "absmiddle" );
  148. /**
  149. * 判断是否是最后一级节点,如果是,递进线为空白
  150. */
  151. if (j+1==level){
  152. if (previousNodes[j-1].firstChild.src.indexOf( this [ "endFolderOpenImg" ])>0){
  153. img.src= this [ "imageURL" ]+ this [ "noneImg" ];
  154. } else {
  155. img.src= this [ "imageURL" ]+ this [ "lineImg" ];
  156. }
  157. } else {
  158. img.src=previousNodes[j-1].src;
  159. }
  160. img.className= this [ "imgClass" ];
  161. cell.appendChild(img);
  162. }
  163. } catch (e){
  164. }
  165. //添加节点事件区域
  166. var  eventArea=document.createElement( "span" );
  167. eventArea.style.cssText= "cursor:pointer;" ;
  168. //加节点展开关闭图片
  169. var  imgOpenOrClose=document.createElement( "img" );
  170. imgOpenOrClose.setAttribute( "align" , "absmiddle" );
  171. imgOpenOrClose.className= this [ "imgClass" ];
  172. imgOpenOrClose.setAttribute( "id" , "sign_img_" +level+ "_" +n_id);
  173. if ((i+1)!=nodeList.size()){
  174. if (ischild && ischild.trim()== "true" ){
  175. imgOpenOrClose.src= this [ "imageURL" ]+ this [ "closeImg" ];
  176. } else {
  177. imgOpenOrClose.src= this [ "imageURL" ]+ this [ "elementLineImg" ];
  178. }
  179. } else {
  180. if (ischild && ischild.trim()== "true" ){
  181. imgOpenOrClose.src= this [ "imageURL" ]+ this [ "endFolderCloseImg" ];
  182. } else {
  183. imgOpenOrClose.src= this [ "imageURL" ]+ this [ "endLineImg" ];
  184. }
  185. }
  186. eventArea.appendChild(imgOpenOrClose);
  187. //添加节点图片
  188. var  imgNodeOpenOrClose=document.createElement( "img" );
  189. imgNodeOpenOrClose.setAttribute( "align" , "absmiddle" );
  190. imgNodeOpenOrClose.className= this [ "imgClass" ];
  191. imgNodeOpenOrClose.setAttribute( "id" , "node_img_" +level+ "_" +n_id);
  192. if ( this [ "useDataImg" ].toLowerCase()== "all" ){
  193. imgNodeOpenOrClose.src= this [ "imageURL" ]+openimg;
  194. } else {
  195. if (ischild && ischild.trim()== "true" ){
  196. imgNodeOpenOrClose.src= this [ "imageURL" ]+ this [ "folderImg" ];
  197. } else {
  198. imgNodeOpenOrClose.src= this [ "imageURL" ]+ this [ "fileImg" ];
  199. }
  200. }
  201. eventArea.appendChild(imgNodeOpenOrClose);
  202. //节点事件参数
  203. var  file_image_param= null ,node_img_open_param= null ,node_img_colse_param= null ;
  204. if ( this [ "useDataImg" ].toLowerCase()== "all" ){
  205. file_image_param=openimg;
  206. node_img_open_param=openimg;
  207. node_img_colse_param=closeimg;
  208. } else {
  209. file_image_param= this [ "fileImg" ];
  210. node_img_open_param= this [ "folderOpenImg" ];
  211. node_img_colse_param= this [ "folderImg" ];
  212. }
  213. var  img_open_param= null ,img_close_param= null ;
  214. if ((i+1)!=nodeList.size().length){
  215. img_open_param= this [ "openImg" ];
  216. &n

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。