网络编程
位置:首页>> 网络编程>> XML编程>> 用ASP和XMLHTTP分析远程XML文件

用ASP和XMLHTTP分析远程XML文件

作者:fanflash 来源:fanflash blog 发布时间:2007-12-12 12:48:00 

标签:XMLDOM,xml,asp

使用本文给出的方法就可以制作出一个简单的rss阅读器了。

用xmldom方法打开xml文件,如果是本地的没有问题,就是用Server.MapPath("xml.xml")的方法,这时能正常分析出内容,但是直接用url却不显示出xml内容(在XMLDOM里表示是支持URL方式的),后来研究一下发现可以用XMLHTTP的方法获取XML后再分析,代码如下:


Set http=Server.CreateObject("Microsoft.XMLHTTP")
http.Open "GET","http://localhost/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
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com