您的位置:首页 > 网络编程 > ASP

ASP中读取远程XML文件的方法 XMLHttp+XMLDOM

日期:2006-08-24 15:13:10 点击: 来自: 作者:

asp 文件

以下是代码片段:
<%
url = "http://localhost:3994/PassPort/xml.xml"

Set http=Server.CreateObject("Microsoft.XMLHTTP")
http.Open "GET",url,False
http.send
Set xml=Server.CreateObject("Microsoft.XMLDOM")
xml.Async=true
xml.ValidateOnParse=False
xml.Load(http.ResponseXML)

If xml.ReadyState>2 Then
  ''xmlhttp.readyState的4个属性
  ''1:LOADING;2:LOADED;3:INTERACTIVE;4:COMPLETED
''Response.Write("文档已经准备就绪。状态:"& xml.ReadyState &"<br>")
  
  if xml.parseError.errorCode <> 0 then
   response.write "错误:读取数据失败"
     response.write xml.parseerror.errorcode
     response.write xml.parseerror.reason
     'xml.loadxml(myxmlstr)
     response.End()
  else
    'response.write "ok"
  end if
  
  
  '可一次性读出所有的值
  Set UserData=xml.getElementsByTagName("userdata")
  if isnull(xml.getElementsByTagName("userdata") ) then
    response.Write("读取失败")
    response.End()
  else
    for j=0 to UserData.item(i).childnodes.length-1
      Response.Write UserData.item(0).childnodes(j).text &"<br>"
    next
  end if
    
  
  '也可使用下面的方法一次只读一个值
  Set title=xml.getElementsByTagName("Uid")
  Response.Write(title.item(0).text &"<br>")
  Response.Write(xml.getElementsByTagName("RealName").item(0).text &"<br>")
  Response.Write(xml.getElementsByTagName("Sex").item(0).text &"<br>")
  Response.Write(xml.getElementsByTagName("Birthday").item(0).text &"<br>")
    
Else
'Response.Write("文档还未准备就绪。状态:"& xml.ReadyState &"<br>")
End If
Set http=Nothing
Set xml=Nothing
%>

xml 文件

以下是代码片段:

 <?xml version="1.0" encoding="utf-8" ?>
- <userdata>
<Uid>用户ID</Uid>
<RealName>真实姓名</RealName>
<Sex>性别</Sex>
<Birthday>生日</Birthday>
<Tel>电话</Tel>
<Fax>传真</Fax>
<Addr>住址</Addr>
<Postcode>邮编</Postcode>
<CompanyName>公司名称</CompanyName>
<CompanyUrl>网址</CompanyUrl>
</userdata>

More..素材图片 Picture Navigation
ASP热门 Class Hot
ASP推荐 Class Commend
版权所有:中国网站资源 2005- 未经授权禁止复制或建立镜像 This Site Tech:XHTML+DIV+CSS+Javascript
CopyRight ® 2005- www.chinaddv.com online services. all rights reserved. ICP06016627
Optimized to 1024x768 to Firefox,Netscape,Opera,MS-IE6+.