111111
精灵王
精灵王
  • 注册日期2010-12-08
  • 发帖数640
  • QQ
  • 火币1103枚
  • 粉丝120
  • 关注75
  • 社区明星
阅读:3757回复:0

用asp和microsoft.xmldom分析远程xml文件_asp技巧

楼主#
更多 发布于:2011-01-26 22:23
   用xmldom方法打开xml文件,如果是本地的没有问题,就是用Server.MapPath("xml.xml")的方法,这时能正常分析出内容,不过直接用url却不显示出xml内容(在XMLDOM里表示是支持URL方式的),后来研究一下发现能用XMLHTTP的方法获取XML后再分析,代码如下:
Set http=Server.CreateObject("Microsoft.XMLHTTP")
http.Open "GET","http://www.knowsky.com/xml.xml",False
http.send

Set xml=Server.CreateObject("Microsoft.XMLDOM")
xml.Async=False
xml.ValidateOnParse=False
xml.Load(http.ResponseXML)
If xml.ReadyState>2 Then
        Response.Write("文件已准备就绪。状态:"; xml.ReadyState ;"<br>")
        Set item=xml.getElementsByTagName("item")
        For i=0 To (item.Length-1)
        Set title=item.Item(i).getElementsByTagName("title")
        Set link=item.Item(i).getElementsByTagName("link")
        Response.Write("<a href="""; link.Item(0).Text ;""">"; title.Item(0).Text ;"</a><br>")
        Next
Else
        Response.Write("文件还未准备就绪。状态:"; xml.ReadyState ;"<br>")
End If
Set http=Nothing
Set xml=Nothing


xml.xml文件的内容如下:
<?xml version="1.0" encoding="utf-8"?>
<channel>
<item>
  <title>测试文件1</title>
  <link>http://localhost/</link>
</item>
<item>
  <title>测试文件2</title>
  <link>http://localhostindex.asp</link>
</item>
</channel>


喜欢0 评分0
游客

返回顶部