<% '///////////////////////////////调 用 实 例/////////////////////////////// ' xmlWrite.asp ' powerBy yans chen ' 调用实例1:以数据集写入 ' set rs = conn.execute("select * from [option] where Sid = " & id) ' set xw = new xmlWrite ' xw.init() ' xw.textColumn = "Content" ' xw.writeWithRs(rs) ' set xw = nothing ' ' 调用实例2:自行建字段写入 ' set xw = new xmlWrite ' xw.init() ' xw.add("a","1") ' xw.add("b","2") ' xw.text = "haha" ' xw.feed() ' set xw = nothing '//////////////////////////////////////////////////////////////////////// class xmlWrite private T 'Dictionary private Text_, documentName_, childName_, textColumn_, a, i publicpropertyget count() count = T.Count end property publicpropertyget documentName() '文档主节点名 documentName = documentName_ end property publicpropertylet documentName(byval val) documentName_ = val end property publicpropertyget childName() '文档子节点名 childName = childName_ end property publicpropertylet childName(byval val) childName_ = val end property publicpropertyget text() text = Text_ end property publicpropertylet text(byval val) Text_ = val end property publicpropertyget textColumn() textColumn = textColumn_ end property publicpropertylet textColumn(byval val) textColumn_ = val end property publicfunction add(name, value) '添加新值 if T.Exists(name) then T.Item(name) =trim(value) else T.Add name, trim(value) endif add = noErr() end function publicfunction getValue(name) if T.Exists(name) then getValue = T.Item(name) endif end function publicfunction del(name) if T.Exists(name) then T.Remove(name) endif del = noErr() end function publicfunction removeAll() T.RemoveAll() removeAll = noErr() end function publicfunction feed() a = T.Keys w "<"& childName_ &"" for i =0to T.Count -1 if T.Item(a(i)) <>""then w a(i) &"="""& server.HTMLEncode(T.Item(a(i))) &""" " else w a(i) &"="""" " endif next w ">"& server.HTMLEncode(Text_) &"</"& childName_ &">" end function publicfunction writeWithRs(rs) '使用记录集写成xml whilenot rs.eof for i =0to rs.Fields.count -1 iftrim(rs.Fields(i).Name) = textColumn_ then text_ = rs.Fields(i).Value else add rs.Fields(i).Name, trim(rs.Fields(i).Value) endif next feed() rs.movenext wend end function publicfunction Init() w "<?xml version=""1.0"" encoding=""gb2312"" ?>" w "<"& documentName_ &"" a = T.Keys for i =0to T.Count -1 if T.Item(a(i)) <>""then w a(i) &"="""& server.HTMLEncode(T.Item(a(i))) &""" " else w a(i) &"="""" " endif next w ">" removeAll() end function privatefunction noErr() if err.number <>0then noErr =true err.Clear() else noErr =false endif end function privatesub Class_Initialize ' 设置 Initialize 事件。[类的初始化] set T = server.CreateObject("Scripting.Dictionary") Text_ ="" textColumn_ ="" documentName_ ="yans" childName_ ="yansChild" end sub privatesub Class_Terminate ' 设置 Terminate 事件。 [类的结束化] w "</"& documentName_ &">" set T =nothing set Text_ =nothing end sub privatefunction w(param) response.Write(param) end function end class %>