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