一组常用的ASP处理XML的常用函数分享,代码直接贴出运行时的输出效果,直接保存为文件运行即可。
开发时用到的一组ASP处理XML数据的常用函数。将XML转换为 请求参数格式,读取XML节点名的值,还有一个函数是将 【请求参数】 转换为 XML
dim str,arrstr,jsons,xmlstr,para
str = "<xml><mch_id><![CDATA[3423342353]]></mch_id><nonce_str><![CDATA[ibuaiVcKdpRxk43hJA]]></nonce_str><sign><![CDATA[F766FB383C5BCAB169E566DF43DFG]]></sign></xml>"
para = "mch_id=alixixi"
arrstr = XMLToArr(str)
response.write arrstr(0) &"<br />"
response.write GetParaValue(arrstr,"sign") &"<br />"
response.write server.HTMLEncode(paraToXML(para)) &"<br />"
'读取XML节点名的值
private function GetParaValue(byval paraArr, byval paraName)
dim i
GetParaValue = ""
for i = 0 to Ubound(paraArr)
if left(paraArr(i), len(paraName) + 1) = paraName & "=" then
GetParaValue = mid(paraArr(i), len(paraName) + 2)
exit function
end if
next
end function
'将XML转换为 请求参数格式
'格式:mch_id=1527172451,nonce_str=ibuaiVcKdpRxkhJA
private function XMLToArr(byval xmlDoc)
dim paraArr(),objXml,objParent,i
Set objXml = Server.CreateObject("MSXML2.DOMDocument")
objXml.resolveExternals = false
objXml.loadxml xmlDoc
set objParent = objXml.SelectNodes("//xml")
if objParent.length > 0 then
redim paraArr(objParent(0).childNodes.length - 1)
for i = 0 to objParent(0).childNodes.length - 1
paraArr(i) = objParent(0).childNodes(i).nodeName & "=" & objParent(0).childNodes(i).text
next
end if
XMLToArr = paraArr
end function
'将 【请求参数】 转换为 XML
private function paraToXML(byval paraItem)
dim nodeName,nodeValue
if instr(paraItem, "=") > 0 then
nodeName = mid(paraItem, 1, instr(paraItem, "=") - 1)
nodeValue = mid(paraItem, instr(paraItem, "=") + 1)
paraToXML = "<" & nodeName & "><![CDATA[" & nodeValue & "]]></" & nodeName & ">"
else
paraToXML = ""
end if
end function
%>