ASP操作XML的类

    虽然痛恨,但没办法,还是要用ASP.于是写了这么个类,缺点肯定很多,但不管了,只要我用着方便就行了.
   
类代码
  1 <%
  2     '******************************************************************************
  3     ' Author : zk
  4     ' Create Time : 2007-11-15
  5     ' Desc : 操作XML对象
  6     '******************************************************************************
  7 %>
  8 <%
  9 Class XmlManage
 10     ' xml 对象实例
 11     Public xmlDoc
 12     
 13     '*************************************************
 14     ' 类的初始化
 15     Private Sub Class_Initialize
 16         set xmlDoc = Server.CreateObject("Msxml2.DOMDocument")
 17         xmlDoc.async = False
 18     End Sub
 19     
 20     '*************************************************
 21     ' 类的析构
 22     Private Sub Class_Terminate
 23         Set xmlDoc = Nothing
 24     End Sub    
 25     
 26     '*************************************************
 27     ' 加载XML
 28     ' oLoad : 
 29     ' iType : 要加载的类型.
 30     '            0,说明oLoad为空,从Request中读取数据;
 31     '            1,说明oLoad为字符串,直接做为XML格式字符串读取即可;
 32     '            2,说明oLoad为XML文件路径,需要得到绝对路径后读取;
 33     '            3,说明oLoad为空,由本程序构造默认的XML格式;
 34     Public Function LoadXml(ByVal oLoad, ByVal iType)
 35         dim bResult
 36         bResult = False
 37         Select Case iType
 38                Case 0
 39                    ' 从Request中读取数据
 40                 xmlDoc.load(Request)
 41                Case 1
 42                    ' 直接做为XML格式字符串读取
 43                 xmlDoc.loadXML(oLoad)
 44                Case 2
 45                 ' 得到绝对路径后读取
 46                 xmlDoc.load(GetAbsolutePath(oLoad))
 47                Case Else
 48                 ' oLoad为空,由本程序构造默认的XML格式
 49                 dim sXml
 50                 sXml = ""
 51                 sXml = sXml & "<FeedbackInfo>"
 52                 sXml = sXml & "<SubList></SubList>"
 53                 sXml = sXml & "</FeedbackInfo>"
 54                 xmlDoc.loadXML(sXml)
 55           End Select
 56         bResult = True
 57         LoadXml = bResult
 58     End Function
 59     
 60     '*************************************************
 61     ' 设置xmlDoc中节点的值(没有重名节点的情况)
 62     Public Function SetSingleNodeText(ByVal sNodeName, ByVal sNodeText)
 63         dim bResult
 64         bResult = False
 65         xmlDoc.selectSingleNode(ReturnSelectName(sNodeName)).text = sNodeText
 66         bResult = True
 67         SetSingleNodeText = bResult
 68     End Function
 69     
 70     '*************************************************
 71     ' 设置xmlDoc中节点的值(同名节点有多个的情况)
 72     Public Function SetNodeTextInNodes(ByVal sNodeName, ByVal sNodeText, ByVal iIndex)
 73         dim bResult
 74         bResult = False
 75         dim nNodeList
 76         set nNodeList = xmlDoc.selectNodes(ReturnSelectName(sNodeName))
 77         if iIndex >= 0 and iIndex < nNodeList.length then
 78             nNodeList(iIndex).text = sNodeText
 79         end if
 80         set nNodeList = nothing
 81         bResult = True
 82         SetNodeTextInNodes = bResult
 83     End Function
 84     
 85     '*************************************************
 86     ' 返回节点(没有重名节点的情况)
 87     Public Function GetSingleNode(ByVal sNodeName)
 88         set GetSingleNode = xmlDoc.selectSingleNode(ReturnSelectName(sNodeName))
 89     End Function
 90     
 91     '*************************************************
 92     ' 根据索引返回节点(同名节点有多个的情况)
 93     Public Function GetNodeByIndex(ByVal sNodeName, ByVal iIndex)
 94         dim nNodeList
 95         set nNodeList = xmlDoc.selectNodes(ReturnSelectName(sNodeName))
 96         if iIndex >= 0 and iIndex < nNodeList.length then
 97             set GetNodeByIndex = nNodeList(iIndex)
 98         end if
 99         set nNodeList = nothing
100     End Function
101     
102     '*************************************************
103     ' 添加节点
104     ' sParentNode : 父节点
105     ' sChildNode : 子节点
106     ' sNodeText : 节点文本
107     Public Function AddNode(ByVal sParentNode,ByVal sChildNode,ByVal sNodeText)
108         dim bResult
109         bResult = False
110         
111         set child = xmlDoc.createElement(sChildNode)
112         if Len(sNodeText) >= 0 then
113             child.text = sNodeText
114         end if
115         ' 用=""判断比较好,还是用IsNull判断比较好,以后可以测试一下
116         if sParentNode = "" then
117             xmlDoc.firstChild.appendchild(child)
118         else
119             xmlDoc.selectSingleNode(ReturnSelectName(sParentNode)).appendchild child
120         end if
121         set child = nothing
122         
123         bResult = True
124         AddNode = bResult
125     End Function
126     
127     '*************************************************
128     ' 添加节点
129     ' sParentNode : 父节点
130     ' sChildNode : 子节点
131     ' sNodeText : 节点文本
132     ' iIndex : 节点索引,即,如果名为sParentNode的父节点有多个,则把子节点添加到索引为iIndex的上面
133     Public Function AddNodeByIndex(ByVal sParentNode,ByVal sChildNode,ByVal sNodeText, ByVal iIndex)
134         dim bResult
135         bResult = False
136         
137         set child = xmlDoc.createElement(sChildNode)
138         if Len(sNodeText) >= 0 then
139             child.text = sNodeText
140         end if
141         ' 用=""判断比较好,还是用IsNull判断比较好,以后可以测试一下
142         if sParentNode = "" then
143             xmlDoc.firstChild.appendchild(child)
144         else
145             if xmlDoc.selectNodes(ReturnSelectName(sParentNode)).length > 1 then
146                 if iIndex >= 0 and iIndex < xmlDoc.selectNodes(ReturnSelectName(sParentNode)).length then
147                     xmlDoc.selectNodes(ReturnSelectName(sParentNode))(iIndex).appendchild child
148                 else
149                     xmlDoc.selectSingleNode(ReturnSelectName(sParentNode)).appendchild child
150                 end if
151             else
152                 xmlDoc.selectSingleNode(ReturnSelectName(sParentNode)).appendchild child
153             end if
154         end if
155         set child = nothing
156         
157         bResult = True
158         AddNodeByIndex = bResult
159     End Function
160     
161     '*************************************************
162     ' 返回节点的子节点数
163     Public Function GetChildrenNumOfNode(ByVal sParentNode)
164         dim iResult
165         iResult = 0
166         iResult = xmlDoc.selectSingleNode(ReturnSelectName(sParentNode)).childNodes.length
167         
168         GetChildrenNumOfNode = iResult
169     End Function
170     
171     '*************************************************
172     ' 保存成文件
173     ' sFileName : 文件名,相对路径
174     Public Function SaveAsFile(ByVal sFileName)
175         dim bResult
176         bResult = False
177         xmlDoc.Save(GetAbsolutePath(sFileName))
178         
179         bResult = True
180         SaveAsFile = bResult
181     End Function
182     
183     '*************************************************
184     ' 添加头
185     Public Function AddHead()
186         dim bResult
187         bResult = False
188         dim oPI
189         Set oPI = xmlDoc.createProcessingInstruction("xml""version=""1.0"" encoding=""GB2312""")
190         xmlDoc.insertBefore oPI,xmlDoc.childNodes(0)
191         Set oPI = nothing
192         
193         bResult = True
194         AddHead = bResult
195     end Function
196     
197     '*************************************************
198     ' 使用selectSingleNode等方法时,为后面的参数添加//
199     Private Function ReturnSelectName(ByVal sNodeName)
200         ReturnSelectName = "//" & sNodeName
201     End Function
202     
203     '*************************************************
204     ' 得到文件的绝对路径
205     Private Function GetAbsolutePath(sFileName)
206         GetAbsolutePath = Server.MapPath(sFileName)
207     End Function
208 End Class
209 %>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值