XML节点信息读取函数

[ 2007-01-11 12:29:11 | 作者: {$author:name$} ]  编辑  删除
字体大小: | |
下载:http://www.x2y2.com/files/getXmlInfo.rar

<%
authorName = getXmlInfo("sample.xml","//sample/author/name")
authorEmail = getXmlInfo("sample.xml","//sample/author/email")
authorHomepage = getXmlInfo("sample.xml","//sample/author/homepage")
response.write "author: "&authorName
response.write "Email:<a href=""mailto:"&authorEmail&""">"&authorEmail&"</a>"
response.write "Homepage:  <a href="""&authorHomepage&""">"&authorHomepage&"</a>"

' *************************************
' 读取XML结点信息
' by fisker( http://www.x2y2.com/fisker )
' 12:30 2007-1-11
' *************************************
Function getXmlInfo(tempXmlFilePath,tempNode)
on error resume next
dim objXMLDOM,styleInfoItem
Set objXMLDOM = Server.CreateObject(getXMLDOM)
If Err Then  
Err.clear
getXmlInfo=""
exit Function
end if
dim tempOutputString
objXMLDOM.async = false  
objXMLDOM.load(Server.MapPath(tempXmlFilePath))
if objXMLDOM.parseerror.errorcode=0 then
For Each styleInfoItem in objXMLDOM.selectNodes(tempNode)
tempOutputString=styleInfoItem.text
next
end if
set objXMLDOM=nothing
getXmlInfo=tempOutputString
end Function
' *************************************
' 判断服务器Microsoft.XMLDOM
' *************************************
Function getXMLDOM
On Error Resume Next
Dim Temp
getXMLDOM="Microsoft.XMLDOM"
Err = 0
Dim TmpObj
Set TmpObj = Server.CreateObject(getXMLDOM)
Temp = Err
    IF Temp = 1 OR Temp = -2147221005 Then
getXMLDOM="Msxml2.DOMDocument.5.0"
End IF
Err.Clear
Set TmpObj = Nothing
Err = 0
end function
' *************************************
' 判断服务器MSXML2.ServerXMLHTTP
' *************************************
Function getXMLHTTP
On Error Resume Next
Dim Temp
getXMLHTTP="MSXML2.ServerXMLHTTP"
Err = 0
Dim TmpObj
Set TmpObj = Server.CreateObject(getXMLHTTP)
Temp = Err
    IF Temp = 1 OR Temp = -2147221005 Then
getXMLHTTP="Msxml2.ServerXMLHTTP.5.0"
End IF
Err.Clear
Set TmpObj = Nothing
Err = 0
end function
%>


12:59 update
写完以后又去查了一下,返回单个节点似乎应当使用selectSingleNode
styleInfoItem=objNode.selectSingleNode(tempNode)
If Not (styleInfoItem is Nothing) then 
tempOutputString=styleInfoItem.text
End If

未测试,应该没有错误
参考:http://www.vchome.net/dotnet/xml/xml3.htm
评论Feed 评论Feed: http://ifisker.com/blog/feed.asp?q=comment&id=855
UTF-8 Encoding 引用链接: loading...

这篇日志没有评论.

发表评论
表情图标
[smile][confused][cool][cry]
[eek][angry][wink][sweat]
[lol][stun][razz][redface]
[rolleyes][sad][yes][no]
[heart][star][music][idea]
UBB代码
转换链接
表情图标
悄悄话
用户名:   密码:   注册?
验证码* 请输入验证码