Ext树形结构动态读取xml数据

Ext.onReady(function(){
    Ext.QuickTips.init();// 浮动信息提示
    Ext.BLANK_IMAGE_URL="ext/resources/images/default/tree/s.gif";

function createXMLDOM()
{
var xmlDoc = null;
//通过对象/属性检测法,判断是IE来是Mozilla
if (!window.DOMParser  && window.ActiveXObject)
{
  var arrVersions= ["MSXML2.DOMDocument.5.0","MSXML2.DOMDocument.4.0",
   "MSXML2.DOMDocument.3.0","MSXML2.DOMDocument", "Microsoft.XmlDoc"];
  for (var i=0; i < arrVersions.length; i++)
  {
try
{
  xmlDoc = new ActiveXObject(arrVersions[i]);
  return xmlDoc;            
}
catch (oError)
{
   //忽略,继续
}
  }          
  throw new Error("系统没有安装MSXML");   
 
   }else if(xmlDoc == null && document.implementation && document.implementation.createDocument)
   {    // Mozilla browsers have a DOMParser
  try{
xmlDoc = document.implementation.createDocument("","",null);
return xmlDoc;
}catch (ex){}
   }else 
   {
   throw   new  Error( " Your browser doesn't support an XML DOM object. " );
   }
}
if(window.DOMParser){
Document.prototype.loadXML = function(sXml){
var oParser= new DOMParser();
var _xmlDom = oParser.parseFromString(sXml, "text/xml");
while(this.firstChild){
   this.removeChild(this.firstChild);
}

for(var i=0;i<_xmlDom.childNodes.length;i++){
var oNewNode = this.importNode(_xmlDom.childNodes[i],true);
this.appendChild(oNewNode);
}
}
}

var gGroupDB = new Array();
function fillGroupDB(xmlDoc)
{
gGroupDB.length = 0;
var groups = xmlDoc.getElementsByTagName("group");

for(var i = 0;i < groups.length;i++)
{
var e = groups[i];
var groupno = getXmlTagValue(e,"groupno");
var groupname = getXmlTagValue(e,"groupname");
var upgroupno = getXmlTagValue(e,"upgroupno");
gGroupDB[gGroupDB.length] = {groupno:groupno,groupname:groupname,upgroupno:upgroupno};
}
}
/*********根据父组号递归创建下属组号的节点**************************************/
function CreateGroupTreeNode(ParentNode,UpGroupNo)
{
var parentElem = document.getElementById("xmltab");
for(var i = 0; i < gGroupDB.length; i++)
{
if(gGroupDB[i].upgroupno == UpGroupNo)
{
var groupno = "";
var groupname = "" ;
groupno = gGroupDB[i].groupno;
groupname = gGroupDB[i].groupname;
var CurNode = new Ext.tree.TreeNode({
text:groupname,
href:'groupusers.html?groupno='+gGroupDB[i].groupno+'&groupname='+gGroupDB[i].groupname,
hrefTarget:'byright',
expanded:true
});
ParentNode.appendChild(CurNode);
CreateGroupTreeNode(CurNode,groupno);
}
}
}
var xmlDoc = null;
var centerno = get_vars(parent.document,"centerno");
    var dispatcherno = get_vars(parent.document,"dispatcherno");
var xml = "<?xml version='1.0' encoding='utf-8'?><root><tablenum>2</tablenum><actionnum>0</actionnum><centerno>"+centerno+"</centerno><dispatcherno>"+dispatcherno+"</dispatcherno></root>";
var xmlDoc = createXMLDOM();
xmlDoc.loadXML(xml);
    Ext.Ajax.request({ url:"cgi-bin/grouplist.jsp",
   xmlData: xmlDoc,
   method:'POST',
   success: function(response,options){ xmlDoc = response.responseXML;

                                                         fillGroupDB(xmlDoc);
                 //生成根节点
var root=new Ext.tree.TreeNode({
id:"root",
text:"通信录",
href:'groupusers.html?groupno=all&groupname=所有',
hrefTarget:'byright',
expanded:true
});

for(var i = 0; i < gGroupDB.length; i++)
{  
if("" == gGroupDB[i].upgroupno || "65535" == gGroupDB[i].upgroupno)
{
//生成根节点下的一级子节点
var childNode=new Ext.tree.TreeNode({
    text:gGroupDB[i].groupname,
href:'groupusers.html?groupno='+gGroupDB[i].groupno+'&groupname='+gGroupDB[i].groupname,
                                        hrefTarget:'byright',
expanded:true
    });
   root.appendChild(childNode);    //向根节点中增加一级子节点
//创建子节点
CreateGroupTreeNode(childNode,gGroupDB[i].groupno);
}
}

var tree=new Ext.tree.TreePanel({
        title:"联系人组列表",
renderTo:"grouptree",
root:root,
border: false,
width:300,
height:582
});
},
   failure: function(response,options){ Ext.MessageBox.alert('错误'); },
  
   scope:this});

});




//xml数据
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*,java.util.*" errorPage="" %><%
response.getWriter().print("<?xml version=\"1.0\" encoding=\"gb2312\"?>");
response.setContentType("text/xml;charset=gb2312");
response.getWriter().print("<root>");
response.getWriter().print("<returnflag>1</returnflag>");
response.getWriter().print("<groups>");
response.getWriter().print("<group><groupno>1</groupno><groupname>同学</groupname><isdspgroup>0</isdspgroup><upgroupno></upgroupno><upgroupname></upgroupname></group>");
response.getWriter().print("<group><groupno>2</groupno><groupname>张三</groupname><isdspgroup>1</isdspgroup><upgroupno>1</upgroupno><upgroupname>dfasdf</upgroupname></group>");
response.getWriter().print("<group><groupno>3</groupno><groupname>李四</groupname><isdspgroup>1</isdspgroup><upgroupno>1</upgroupno><upgroupname>dfasdf</upgroupname></group>");
response.getWriter().print("<group><groupno>4</groupno><groupname>aabbhhhhhhhhhh</groupname><isdspgroup>0</isdspgroup><upgroupno></upgroupno><upgroupname></upgroupname></group>");
response.getWriter().print("<group><groupno>5</groupno><groupname>bbbbhhhhhhhhhh</groupname><isdspgroup>1</isdspgroup><upgroupno>4</upgroupno><upgroupname>dfasdf</upgroupname></group>");
response.getWriter().print("</groups>");
response.getWriter().print("</root>");
response.setHeader("Cache-Control","no-cache");
response.getWriter().close();
%>

你可能感兴趣的:(数据结构,xml,Ajax,ext,IE)