xmlwriter,asp的xml生成类

<%
'///////////////////////////////调 用 实 例///////////////////////////////
'
                                                           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

    
public property get count()
        count 
= T.Count
    
end property
    
    
public property get documentName()          '文档主节点名
        documentName = documentName_
    
end property
    
public property let documentName(byval val)
        documentName_ 
= val
    
end property
    
public property get childName()             '文档子节点名
        childName = childName_
    
end property
    
public property let childName(byval val)
        childName_ 
= val
    
end property
    
public property get text()
        text 
= Text_
    
end property
    
public property let text(byval val)
        Text_ 
= val
    
end property
    
public property get textColumn()
        textColumn 
= textColumn_
    
end property
    
public property let textColumn(byval val)
        textColumn_ 
= val
    
end property
    
    
public function add(name, value)        '添加新值
        if T.Exists(name) then
            T.Item(name) 
= trim(value)
        
else
            T.Add name, 
trim(value)
        
end if
        add 
= noErr()
    
end function
    
    
public function getValue(name)
        
if T.Exists(name) then
            getValue 
= T.Item(name)
        
end if
    
end function

    
public function del(name)
        
if T.Exists(name) then
            T.Remove(name)
        
end if
        del 
= noErr()
    
end function

    
public function removeAll()
        T.RemoveAll()
        removeAll 
= noErr()
    
end function
    
    
public function feed()
        a 
= T.Keys
        w 
"<" & childName_ & " "
        
for i = 0 to T.Count - 1
            
if T.Item(a(i)) <> "" then
                w a(i) 
& "=""" & server.HTMLEncode(T.Item(a(i))) & """ "
            
else
                w a(i) 
& "="""" "
            
end if
        
next
        w 
">" & server.HTMLEncode(Text_) & "</" & childName_ & ">"
    
end function
    
    
public function writeWithRs(rs)     '使用记录集写成xml
        while not rs.eof
            
for i = 0 to rs.Fields.count - 1
                
if trim(rs.Fields(i).Name) = textColumn_ then
                    text_ 
= rs.Fields(i).Value
                
else
                    add rs.Fields(i).Name, 
trim(rs.Fields(i).Value)
                
end if
            
next
            feed()
            rs.movenext
        
wend
    
end function

    
public function Init()
        w 
"<?xml version=""1.0"" encoding=""gb2312"" ?>"
        w 
"<" & documentName_ & " "
        a 
= T.Keys
        
for i = 0 to T.Count - 1
            
if T.Item(a(i)) <> "" then
                w a(i) 
& "=""" & server.HTMLEncode(T.Item(a(i))) & """ "
            
else
                w a(i) 
& "="""" "
            
end if
        
next
        w 
">"
        removeAll()
    
end function
    
private function noErr()
        
if err.number <> 0 then
            noErr 
= true
            err.Clear()
        
else
            noErr 
= false
        
end if
    
end function 
    
private sub Class_Initialize    ' 设置 Initialize 事件。[类的初始化]
        set T = server.CreateObject("Scripting.Dictionary")
        Text_ 
= ""
        textColumn_ 
= ""
        documentName_ 
= "yans"
        childName_ 
= "yansChild"
    
end sub
    
private sub Class_Terminate     ' 设置 Terminate 事件。 [类的结束化]
        w "</" & documentName_ & ">"
        
set T = nothing
        
set Text_ = nothing
    
end sub
    
private function w(param)
        response.Write(param)
    
end function 
end class

%
>
posted @ 2008-06-15 08:42  yans  阅读(631)  评论(0编辑  收藏  举报