JavaScript解析XML文档成树菜单
1.解析xml的BlueParseXML.js文件
- /**
- * @description 解析并封装XML
- * @author BluesLee
- * @lastModif BluesLee
- * @createDate 2007-10-13
- * @modifDate 2007-10-15
- * @version 1.0
- */
- //解析XML文件对象构造器
- function BlueParseXML(){
- }
- BlueParseXML.prototype.xml; //定义对象属性xml
- BlueParseXML.prototype.loadFinish; //加载完成函数
- BlueParseXML.prototype.nodeProperties= new Array(); //节点属性数组
- BlueParseXML.prototype.key= "key" ;
- BlueParseXML.prototype.value= "value" ;
- BlueParseXML.prototype.nodeList; //节点集合
- BlueParseXML.prototype.rootName;
- BlueParseXML.XML_NULL=-1; //未加载xml,或加载失败
- //加载xml文件
- BlueParseXML.prototype.loadXML= function (url){
- var newXML;
- var obj= this ;
- //如果它受支持,采用标准的2级DOM技术
- if (document.implementation && document.implementation.createDocument){
- //创建新的Document对象
- //第一个参数是包含文档所使用的命名空间URI的字符串
- //第二个参数是包含文档根元素名称的字符串
- //第三个参数是要创建的文档类型(也称为doctype)。
- //现在Firefox中并不提供针对文档类型的JavaScript支持,所以第三个参数总是为null。
- newXML=document.implementation.createDocument( "" , "" , null );
- //设置装载完毕时触发事件
- newXML.onload= function (){
- if (obj.loadFinish){
- obj.loadFinish();
- }
- }
- newXML.load(url);
- } else { //IE浏览器创建Document对象
- newXML= new ActiveXObject( "Microsoft.XMLDOM" );
- //设置onload
- newXML.onreadystatechange= function (){
- if (newXML.readyState==4){
- if (obj.loadFinish){
- obj.loadFinish();
- }
- }
- }
- newXML.load(url);
- }
- this .xml=newXML;
- }
- //解析遍历xml
- BlueParseXML.prototype.parseXML= function (){
- if (! this .xml){
- alert( "XML未加载,请先加载XML文件!" );
- return parseXML.XML_NULL;
- }
- var rootNode= this .xml.documentElement;
- this .rootName=rootNode.tagName;
- this .nodeList= new ChildNodeList();
- this .level= this .iteratorNode( this .nodeList,rootNode,1);
- return this .level;
- }
- //遍历节点,将节点信息封装存入ChildNodeList中
- BlueParseXML.prototype.iteratorNode= function (list,node,level){
- if (!isNaN(level) && level>0){
- list.nodeLevel=level;
- }
- var result=list.nodeLevel; //节点级别
- var flag= true ; //判断节点是否有子节点标记
- for ( var child=node.firstChild;child!= null ;child=child.nextSibling){
- if (document.implementation && document.implementation.createDocument && !child.getAttribute){
- continue ;
- }
- flag= false ; //有子节点
- if (!list.nodeName || list.nodeName== "" ){
- list.nodeName=child.tagName;
- }
- if (list.nodeName==child.tagName){
- var nodeInfo= new NodeInfo();
- nodeInfo.nodeLevel=list.nodeLevel; //设置节点级别
- nodeInfo.nodeName=list.nodeName; //设置节点名称
- nodeInfo.key=child.getAttribute( this .key); //设置节点属性
- nodeInfo.value=child.getAttribute( this .value);
- for ( var index=0;index< this .nodeProperties.length;index++){
- nodeInfo[ this .nodeProperties[index]]=child.getAttribute( this .nodeProperties[index]);
- }
- list.addElement(nodeInfo.key,nodeInfo); //把节点加入父节点的子节点集合
- var n= this .iteratorNode(nodeInfo.childNodeList,child,list.nodeLevel+1);
- if (n>result){ //如果子节点返回的节点级别大于改节点级别,设置返回节点级别为最大级别
- result=n;
- }
- }
- }
- if (flag){ //当改节点没有子节点时返回的节点级别-1
- result=result-1;
- }
- return result;
- }
- //扩展String对象,增加trim方法去掉字符串前后空格
- String.prototype.trim= function (){
- return this .replace(/^\s*/g, "" ).replace(/\s*$/g, "" );
- }
- /**
- * @description 自定义集合对象(利用对象属性键值对,类似Hashtable使用)
- * @author BluesLee
- * @lastModif BluesLee
- * @createDate 2007-10-13
- * @modifDate 2007-10-15
- * @version 1.0
- */
- ChildNodeList.prototype.nodeLevel=1;
- ChildNodeList.prototype.nodeName;
- ChildNodeList.prototype.keyArray;
- ChildNodeList.prototype.ELEMENT_NULL=-1; //元素为空
- ChildNodeList.prototype.ELEMENT_EXIST=-2; //元素已经存在
- ChildNodeList.prototype.ELEMENT_NOT_EXIST=-3; //元素不存在
- ChildNodeList.prototype.INDEX_OUT=-4 //数组下标越界
- ChildNodeList.prototype.ERROR_DELETE=-5 //删除元素出错
- function ChildNodeList(){
- this .keyArray= new Array();
- }
- //添加集合元素
- ChildNodeList.prototype.addElement= function (key,element){
- if (!key || !element){
- return ChildNodeList.ELEMENT_NULL;
- }
- if ( this [key]){
- return ChildNodeList.ELEMENT_EXIST;
- }
- var index= this .keyArray.length;
- this .keyArray[index]=key;
- this [key]=element;
- return index;
- }
- //根据下标删除集合元素
- ChildNodeList.prototype.deleteElementByIndex= function (index){
- if (index>= this .keyArray.length){
- return ChildNodeList.INDEX_OUT;
- }
- var key= this .keyArray[index];
- for ( var i=index;i< this .keyArray.length;i++){
- this .keyArray[i]= this .keyArray[i+1];
- }
- this .keyArray.length= this .keyArray.length-1;
- delete this [key];
- return index;
- }
- //根据key删除集合元素
- ChildNodeList.prototype.deleteElementByKey= function (key){
- if (! this [key]){
- return ChildNodeList.ELEMENT_NOT_EXIST;
- }
- for ( var index=0;index< this .keyArray.length;index++){
- if ( this .keyArray[index]==key){
- return this .deleteElementByIndex(index);
- }
- }
- return ChildNodeList.ERROR_DELETE;
- }
- //根据key查找元素
- ChildNodeList.prototype.getElementByKey= function (key){
- return this [key];
- }
- //根据下标查找元素
- ChildNodeList.prototype.getElementByIndex= function (index){
- return this .getElementByKey( this .keyArray[index]);
- }
- //获取集合大小
- ChildNodeList.prototype.size= function (){
- return this .keyArray.length;
- }
- /**
- * @description 节点信息封装对象
- * @author BluesLee
- * @lastModif BluesLee
- * @createDate 2007-10-13
- * @modifDate 2007-10-15
- * @version 1.0
- */
- NodeInfo.prototype.nodeLevel;
- NodeInfo.prototype.nodeName; //节点名称
- NodeInfo.prototype.key; //节点名称,对应xml节点key属性
- NodeInfo.prototype.value; //节点内容,对应xml节点value属性
- NodeInfo.prototype.childNodeList; //子节点列表,用ChildNodeList对象封装
- //构造器
- function NodeInfo(){
- this .childNodeList= new ChildNodeList();
- }
2.BlueTree.js
- /**
- * xml树对象 <br>
- * Author:BluesLee <br>
- * CreateDate:2008-7-9 <br>
- * Modifier:BluesLee <br>
- * ModifyDate:2008-7-9 <br>
- * Version:1.1<br>
- * Copyright(c)2008 深蓝工作室<br>
- * All right reserved.<br>
- *
- */
- BlueTree.prototype.nodeArrayList; //节点总集合
- BlueTree.prototype.isAddOnChangeEvent= false ;
- BlueTree.prototype.SELECTLIST_NULL=-1; //下拉对象数组为空
- BlueTree.prototype.SELECTLIST_TYPE_NOT_ARRAY=-2; //下拉对象数组不是数组对象
- BlueTree.prototype.SELECTLIST_EMPTY=-3; //下拉对象数组为空数组
- BlueTree.prototype.SELECTLIST_LENGTH_OUT=-4; //下拉对象数组小于节点集合级别
- BlueTree.prototype.NODELIST_EMPTY=-5; //节点集合为空
- BlueTree.prototype.EVENT_NULL=-6; //事件源为空
- //树属性
- BlueTree.prototype.rootId; //根节点ID
- BlueTree.prototype.blueURL; //点击节点超连接---isUseDataURL为false时有用
- BlueTree.prototype.isUseDataURL; //是否使用节点封装对象的节点超连接
- BlueTree.prototype.rootName; //根节点名称
- BlueTree.prototype.imageURL; //所有图片目录路径
- BlueTree.prototype.rootImage; //根节点图片
- BlueTree.prototype.folderImg; //有子节点关闭图片
- BlueTree.prototype.folderOpenImg; //有子节点展开图片
- BlueTree.prototype.fileImg; //无子节点图片
- BlueTree.prototype.openImg; //节点展开图片
- BlueTree.prototype.closeImg; //节点关闭图片
- BlueTree.prototype.elementLineImg; //带有节点的线图片
- BlueTree.prototype.lineImg; //无节点的线图片
- BlueTree.prototype.endLineImg; //最后一个节点线图片
- BlueTree.prototype.endFolderOpenImg; //最后一个有子节点展开图片
- BlueTree.prototype.endFolderCloseImg; //最后一个有子节点关闭图片
- BlueTree.prototype.noneImg; //空白图片
- BlueTree.prototype.isFolderURL; //是否给有子节点的节点加超连接---isUseDataURL为false时有用
- BlueTree.prototype.rootURL; //根节点连接
- BlueTree.prototype.target; //所有连接的target属性
- BlueTree.prototype.linkClass; //超连接样式
- BlueTree.prototype.styleClass; //总样式
- BlueTree.prototype.imgClass; //图片样式
- BlueTree.prototype.useDataImg; //节点图片是否使用数据类图片,no为不使用,first为仅第一级目录使用,all全部使用
- BlueTree.prototype.onMouseOver; //鼠标经过每个节点时的样式
- BlueTree.prototype.onMouseOut; //鼠标离开每个节点时的样式
- BlueTree.prototype.isCheckBox; //是否带复选框控件
- BlueTree.prototype.checkBoxName; //复选框控件name
- //构造器
- function BlueTree(nodeList){
- if (nodeList){
- this .nodeArrayList=nodeList;
- } else {
- this .nodeArrayList= new ChildNodeList();
- }
- //初始化树属性
- this .rootId = "0" ;
- this .blueURL = "javascript:void(0);" ;
- this .isUseDataURL = "false" ;
- this .rootName = "根节点" ;
- this .rootImage = "rootImage.gif" ;
- this .imageURL = "images/" ; //必须设置
- this .folderImg = "folder.gif" ;
- this .folderOpenImg = "folderOpen.gif" ;
- this .fileImg = "file.gif" ;
- this .openImg = "open.gif" ;
- this .closeImg = "close.gif" ;
- this .elementLineImg = "elementLine.gif" ;
- this .lineImg = "line.gif" ;
- this .endLineImg = "endLine.gif" ;
- this .endFolderOpenImg = "endFolderOpen.gif" ;
- this .endFolderCloseImg = "endFolderClose.gif" ;
- this .noneImg = "none.gif" ;
- this .isFolderURL = "true" ;
- this .rootURL = "javascript:void(0);" ;
- this .target = "_blank" ;
- this .linkClass = "" ;
- this .imgClass = "" ;
- this .styleClass = "" ;
- this .useDataImg = "no" ;
- this .onMouseOver = "#CCCCCC" ;
- this .onMouseOut = "" ;
- this .isCheckBox = "true" ;
- this .checkBoxName = "checkbox" ;
- }
- /**
- * 根据节点集合构建树 <br>
- * Author:李鹏 <br>
- * CreateDate:2008-9-19 <br>
- * Modifier:李鹏 <br>
- * ModifyDate:2008-9-19 <br>
- * Version:1.1<br>
- *
- * @param parentObject,树所隶属父节点
- * @param nodeList,即将展开节点集合
- */
- BlueTree.prototype.createTree= function (parentObject,nodeList){
- parentObject.innerHTML= "" ;
- if (!nodeList){
- nodeList= this .nodeArrayList;
- }
- var level=1;
- var tableNode=document.createElement( "table" );
- tableNode.className= this [ "styleClass" ];
- tableNode.style.cssText= "border-collapse:collapse;border:0px solid;padding:0px;margin:0px;" ;
- tableNode.border=0;
- tableNode.cellPadding=0;
- tableNode.cellSpacing=0;
- //创建第N级节点
- for ( var i=0;i<nodeList.size();i++){
- var nodeInfo=nodeList.getElementByIndex(i);
- var n_id= "" ,name= "" ,url= "" ,ischild= "false" ,openimg= "" ,closeimg= "" ;
- if (nodeInfo.key){
- n_id=nodeInfo.key;
- }
- if (nodeInfo.value){
- name=nodeInfo.value;
- }
- if (nodeInfo.url){
- url=nodeInfo.url;
- }
- if (nodeInfo.childNodeList.size()>0){
- ischild= "true" ;
- }
- if (nodeInfo.openimg){
- openimg=nodeInfo.openimg;
- }
- if (nodeInfo.closeimg){
- closeimg=nodeInfo.closeimg;
- }
- if (nodeInfo.nodeLevel && nodeInfo.nodeLevel!=level){
- level=nodeInfo.nodeLevel;
- }
- //添加行
- var row=tableNode.insertRow(tableNode.rows.length);
- addEvent(row, "mouseover" ,mouseOverNode);
- addEvent(row, "mouseout" ,mouseOutNode);
- //添加单元格
- var cell=row.insertCell(row.length);
- cell.style.cssText= "white-space:nowrap;" ;
- //根据节点级别加递进线图片
- var previousNodes;
- try {
- previousNodes=parentObject.parentNode.previousSibling.firstChild.childNodes;
- for ( var j=1;j<level;j++){
- var img=document.createElement( "img" );
- img.setAttribute( "align" , "absmiddle" );
- /**
- * 判断是否是最后一级节点,如果是,递进线为空白
- */
- if (j+1==level){
- if (previousNodes[j-1].firstChild.src.indexOf( this [ "endFolderOpenImg" ])>0){
- img.src= this [ "imageURL" ]+ this [ "noneImg" ];
- } else {
- img.src= this [ "imageURL" ]+ this [ "lineImg" ];
- }
- } else {
- img.src=previousNodes[j-1].src;
- }
- img.className= this [ "imgClass" ];
- cell.appendChild(img);
- }
- } catch (e){
- }
- //添加节点事件区域
- var eventArea=document.createElement( "span" );
- eventArea.style.cssText= "cursor:pointer;" ;
- //加节点展开关闭图片
- var imgOpenOrClose=document.createElement( "img" );
- imgOpenOrClose.setAttribute( "align" , "absmiddle" );
- imgOpenOrClose.className= this [ "imgClass" ];
- imgOpenOrClose.setAttribute( "id" , "sign_img_" +level+ "_" +n_id);
- if ((i+1)!=nodeList.size()){
- if (ischild && ischild.trim()== "true" ){
- imgOpenOrClose.src= this [ "imageURL" ]+ this [ "closeImg" ];
- } else {
- imgOpenOrClose.src= this [ "imageURL" ]+ this [ "elementLineImg" ];
- }
- } else {
- if (ischild && ischild.trim()== "true" ){
- imgOpenOrClose.src= this [ "imageURL" ]+ this [ "endFolderCloseImg" ];
- } else {
- imgOpenOrClose.src= this [ "imageURL" ]+ this [ "endLineImg" ];
- }
- }
- eventArea.appendChild(imgOpenOrClose);
- //添加节点图片
- var imgNodeOpenOrClose=document.createElement( "img" );
- imgNodeOpenOrClose.setAttribute( "align" , "absmiddle" );
- imgNodeOpenOrClose.className= this [ "imgClass" ];
- imgNodeOpenOrClose.setAttribute( "id" , "node_img_" +level+ "_" +n_id);
- if ( this [ "useDataImg" ].toLowerCase()== "all" ){
- imgNodeOpenOrClose.src= this [ "imageURL" ]+openimg;
- } else {
- if (ischild && ischild.trim()== "true" ){
- imgNodeOpenOrClose.src= this [ "imageURL" ]+ this [ "folderImg" ];
- } else {
- imgNodeOpenOrClose.src= this [ "imageURL" ]+ this [ "fileImg" ];
- }
- }
- eventArea.appendChild(imgNodeOpenOrClose);
- //节点事件参数
- var file_image_param= null ,node_img_open_param= null ,node_img_colse_param= null ;
- if ( this [ "useDataImg" ].toLowerCase()== "all" ){
- file_image_param=openimg;
- node_img_open_param=openimg;
- node_img_colse_param=closeimg;
- } else {
- file_image_param= this [ "fileImg" ];
- node_img_open_param= this [ "folderOpenImg" ];
- node_img_colse_param= this [ "folderImg" ];
- }
- var img_open_param= null ,img_close_param= null ;
- if ((i+1)!=nodeList.size().length){
- img_open_param= this [ "openImg" ];
- &n