function OpenXml(filename) dim strSourceFile ,XmlDoc,strError strSourceFile = filename Set XmlDoc = Server.CreateObject("Microsoft.XMLDOM") '创建XMLDOM实例 XmlDoc.async = false XmlDoc.load(strSourceFile) OpenXml=XmlDoc.parseerror.errorcode if XmlDoc.parseerror.errorcode<>0 then strError="
error"&XmlDoc.parseerror.errorcode&"
" strError=strError&XmlDoc.parseerror.reason&" " strError=strError&XmlDoc.parseerror.url&" " strError=strError&XmlDoc.parseerror.line&" " strError=strError&XmlDoc.parseerror.filepos&" " strError=strError&XmlDoc.parseerror.srcText&" " response.write strError '输出错误 else set OpenXml=XmlDoc '返回实例 end if end function
'------------------------------------------------ '函数名字:CloseXml() '参数: XmlDoc XML组件实例 '------------------------------------------------ function CloseXml(XmlDoc) if IsObject(XmlDoc) then set XmlDoc=nothing end if end function
'------------------------------------------------ '函数名字:SelectXmlNode '参数:XmlDoc XML组件实例 ' e 元素的名字 '返回元素实例 '------------------------------------------------ function SelectXmlNode(XmlDoc,e) dim n
set n=XmlDoc.selectSingleNode("//" & e ) set selectXmlNode= n
end function
Dim n,np,MaxGroup,root,xmlDoc,nt,filename,s,ss,TorF filename=server.mappath("demo.xml") set xmlDoc=openXML(filename)'打开XML RemoveAllNodes xmlDoc,"Root"'把Root和它下面的子项清除,这样可以多次方便读写 set root= xmlDoc.createElement("Root") xmlDoc.appendChild root'创建一个顶层元素 sql="select Max(GroupID) from tbl_Class " '读出最大的层次 set rs=cn.execute(sql) if isnull(rs(0)) then MaxGroup=0 else MaxGroup=rs(0) '如果为null 表示没有数据 s="":ss="" set nt=xmldoc.createElement("item") nt.setAttribute "text", "请选择" root.appendChild nt '创建一个元素 for i=1 to MaxGroup '开始循环 sql="select * from tbl_Class where GroupID=" & i '由底层向顶层读取 set rs=cn.execute(sql) TorF=False '为了每一个层上都创建一个“请选择”的空取。 do while rs.eof =false '开始读取下层的数据 Set n = xmlDoc.createElement("item" & rs("ClassID")) '创建一个命名为item + ID号的标记元素 n.setAttribute "text",rs("ClassName")'把它的属性“text”设置为数据库表内的
ClassID if rs("ParentID")>0 then '是有上层数据的 set np=selectXmlNode(xmlDoc,"item" & rs("ParentID")) '把它的上层数据元素先读出保存在np if TorF=false then '如果TorF为False值时 set nt=xmldoc.createElement("item") '创建一个元素保存在nt nt.setAttribute "text", "请选择"'把它的text属性设置为“请选择” np.appendChild nt 'np把它加为子元素 end if TorF=true '设置true np.appendChild n 'np 把n加为子元素 else root.appendChild n '如果是第一层数据就把它加入为root下的一个子元素 end if rs.movenext '下一条指针 loop
//-----------------------------------------------------/ // NichName :fason // Autho :Forbes Pu(蒲佛信) // Email :fason_pfx@hotmail.com // HomePage :http://fason.nease.net // Blog :http://blog.mvpcn.net/fason/ // http://blog.csdn.net/fason/ // // function :attachSelect(sXMLSrc, aSel[, sStore]) // param1 :sXMLSrc(URL of XML data source) // param2 :aSel(array of SELECT controls's ID) // param3 :array of default value when display //-----------------------------------------------------/
function attachSelect(sXMLSrc, aSel, sStore) { var oXML = CreateXmlDocument(); var oDocument = null; var store = sStore ? sStore : []; var Sel = new Array();
for (var i=0; i Sel[i] = document.getElementById(aSel[i]);
if (!oXML){ throw new Error('Not support!\nplease install a XML parser');return; } oXML.onreadystatechange = function () { if (oXML.readyState == 4) { attachXML(); } }; oXML.load(sXMLSrc);
function CreateXmlDocument() { if (document.implementation && document.implementation.createDocument) { var doc = document.implementation.createDocument("", "", null); if (doc.readyState == null) { doc.readyState = 1; doc.addEventListener("load", function () { doc.readyState = 4; if (typeof doc.onreadystatechange == "function") doc.onreadystatechange(); }, false); } return doc; } else if (window.ActiveXObject) { var prefix = ["MSXML3","MSXML2","MSXML","Microsoft"]; for (var i=0;i try { var doc = new ActiveXObject(prefix[i] +
function attachXML() { oDocument = oXML.documentElement; if (!oDocument) { throw new Error('No XML data!'); return; } for (var i=0;i addEvent(Sel[i], "onchange", function(x) { return function () { doChange(x); } }(i+1)); } doChange(0); };
function selectXMLNode(x) { var sPath = "/*"; var oDoc = oDocument; for (var i=0;i sPath += "/*[" + (Sel[i].selectedIndex+1) + "]"; if (typeof(oDoc.selectSingleNode)!='undefined') return
oDoc.selectSingleNode(sPath); else { var doc=oDoc.nodeType==9?oDoc:oDoc.ownerDocument; var res = doc.createNSResolver(oDoc.nodeType==9?
function addEvent(el, sHandler, fnc) { if (el.attachEvent) { el.attachEvent(sHandler, fnc); } else if (el.addEventListener) { el.addEventListener(sHandler.replace(/on/i, ''), fnc, false); } else { el[sHandler] = fnc; } };
function doChange(n) { var el = selectXMLNode(n); var nodes = el ? el.childNodes :[]; var s = Sel[n]; var f = 0; if (nodes.length>0) { with (s){ length = 0; for (var i = 0,j = 0;i if (nodes[i].nodeType!=1)continue; var t = nodes[i].getAttribute("text"); var v = nodes[i].getAttribute("value") ? nodes
[i].getAttribute("value") : t; if (v == store[n]) f = j; options[j++] = new Option(t, v); } options[f].selected = true; } if (++n } else { for (var i=n; i with (Sel[i]) { length = 0; options[0] = new Option('--'); options[0].selected = true; } } } }; };