本日志标题:Javascript、ASP解析(远程)XML        [2006-6-13] Bookmark and Share

0,有如下的XML文档(t1.xml):


<?xml version="1.0" encoding="gb2312"?>
<channel>
<item>
<title>Myblog</title>
<link>http://www.cnbruce.com/</link>
</item>
<item>
<title>164Music</title>
<link>http://www.164.cc/</link>
</item>
</channel>



http://www.cnbruce.com/test/testxml/t1.xml

1,可通过javascript建立XMLDOM来解析该XML,方法如下:


[Ctrl+A 全部选择进行拷贝 提示:可先修改部分代码,再点击运行]

注意:xmlDoc.load("/test/testxml/t1.xml") 解析的XML文档必须是在同站点目录下,比如当前保存在我blog内的XML文件即是:

http://www.cnbruce.com/test/testxml/t1.xml

保持XML文件和解析文件在同一站点,而不能垮区域进行。

3,此外,通过Javascript建立的MSXML组建可应用如下:


[Ctrl+A 全部选择进行拷贝 提示:可先修改部分代码,再点击运行]

当然,我对于 ActiveXObject("Microsoft.XMLDOM")、ActiveXObject("MSXML") 以及ActiveXObject("MSXML2.DOMDocument")的定义目前还有些困惑:(

4,如果是ASP脚本是VBScipt的情况来解析该XML,可如下:



<%
Set xml=Server.CreateObject("Microsoft.XMLDOM")
xml.Async=False
xml.ValidateOnParse=False
xml.Load(server.mappath("t1.xml"))

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(title.Item(0).Text &" "& link.Item(0).Text &"<hr />")
Next
%>



其中 server.mappath("t1.xml") 表示解析文件与XML文件的相同目录下。比如

http://www.cnbruce.com/test/testxml/t1.asp

就是解析的相同目录下的

http://www.cnbruce.com/test/testxml/t1.xml

5,来解决下跨越解析的问题
说道跨域,就必然谈到 Microsoft.XMLHTTP

那么先请求远程的任何文件,通过responseXML属性将返回消息为XML文档。

比如:


Set http=Server.CreateObject("Microsoft.XMLHTTP")
http.Open "GET","http://dw8.cn/common/dw8.xml",False
http.send



那么 http.ResponseXML 则是XML数据(如果远程文件本身含有XML数据的话)

所以在4方法的基础上添加远程获取功能,即可实现跨越解析XML数据。



<%
Set http=Server.CreateObject("Microsoft.XMLHTTP")
http.Open "GET","http://dw8.cn/common/dw8.xml",False
http.send


Set xml=Server.CreateObject("Microsoft.XMLDOM")
xml.Async=False
xml.ValidateOnParse=False
xml.Load(http.ResponseXML)

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(title.Item(0).Text &" "& link.Item(0).Text &"<hr />")
Next
%>



调试文件: http://www.cnbruce.com/test/testxml/t2.asp

最后,搜索到有关Microsoft.XMLDOM的属性和方法定义如下:

[Ctrl+A 全部选择进行拷贝 提示:可先修改部分代码,再点击运行]

文章来源: http://www.adp-gmbh.ch/web/js/msxmldom/methods_properties.html

当然,关于远程解析XML,还可通过XSL,这在我的一篇日志中提及的:

http://www.cnbruce.com/blog/showlog.asp?cat_id=34&log_id=969






[Edit on 2006-6-13 18:15:10 By cnbruce]

在百度搜索完整的Javascript、ASP解析(远程)XML内容,或者用Google搜索相关的更多内容

By [cnbruce] at 18:11:25 | 评论 [7] | 浏览 [19663] | TrackBack| 返回顶部

About Comments
如果我要获取一个标签比如<description>里的一段HTML代码内的某个标签怎么做?
By [花老鼠] at 2006-6-18 7:43:49
把以下片段改为javascript为脚本的asp函数,改如何写:
regEx.Pattern = "DATEDIFF\([\s]?(second|minute|hour|day|month|year)[\s]?\,[\s]?(.+?)[\s]?\,[\s]?(.+?)([\s]?\)[\s]?)"
Set Matches = regEx.ExeCute(Sql)
Dim temStr
For Each Match In Matches
temStr = "DATEDIFF("
Select Case lcase(Match.SubMatches(0))
Case "second" :
temStr = temStr & "'s'"
Case "minute" :
temStr = temStr & "'n'"
Case "hour" :
temStr = temStr & "'h'"
Case "day" :
temStr = temStr & "'d'"
Case "month" :
temStr = temStr & "'m'"
Case "year" :
temStr = temStr & "'y'"
End Select
temStr = temStr & "," & Match.SubMatches(1) & "," & Match.SubMatches(2) & Match.SubMatches(3)
Sql = Replace(Sql,Match.Value,temStr,1,1)
Next
By [372100] at 2007-10-18 15:55:40
http://www.cnbruce.com/blog/showlog.asp?cat_id=34&log_id=969 <a href=http://www.cnbruce.com/blog/showlog.asp?cat_id=34&log_id=969>xass</a>
By [Millsberry] at 2008-1-5 5:52:27
Welcome to your next beauty destination!
Founded in 2014 by a female entrepreneur in Dubai with a strong vision that every person is unique and needs his beauty treat to fulfill his ultimate potential, an Art of Beauty Center has become a preferred style destination for both expats and residents in Dubai.
Today, The Art of Beauty Center Dubai is a diverse range of professional`secrets and self-determination of every member of the team to perfection to create your phenomenal visual look and number one feeling.

Site: https://artofbeautycenter.ae/about/
Location: 1017b Al Wasl Road, Umm Suqeim 2, Manara Area, Dubai, United Arab Emirates
Phone: +971 4 344 4004
Online Booking: https://artofbeautycenter.ae/online-consultation/
By [HollieRob] at 2018-9-16 10:54:19
Welcome to your next beauty destination!
Founded in 2014 by a female entrepreneur in Dubai with a strong vision that every person is unique and needs his beauty treat to fulfill his ultimate potential, an Art of Beauty Center has become a preferred style destination for both expats and residents in Dubai.
Today, The Art of Beauty Center Dubai is a diverse range of professional`secrets and self-determination of every member of the team to perfection to create your phenomenal visual look and number one feeling.

Site:
http://artofbeautycenter.ae/nails/
Location: 1017b Al Wasl Road, Umm Suqeim 2, Manara Area, Dubai, United Arab Emirates
Phone: +971 4 344 4004
Online Booking: https://artofbeautycenter.ae/online-consultation/
By [HollieRob] at 2018-9-16 10:54:20

hello!
Welcome to your next beauty destination!
“Art of Beauty” brand was the first one in the UAE. Despite the difference in cultural and linguistic barriers, as well as social norms of a new country, we are offering an ultimate approach for a beauty care as an essential part of individual health care. It`s one of the selected centers, that offers a wide range of face, hair, and body treatments for all- ladies, gentlemen, and children.
Site: https://nailsandhair.artofbeautycenter.ae
Phone: +971 55 173 6778
Location: 1017b, Al Wasl Road, Umm Suqeim 2, Manara Area - Dubai, UAE
By [BettyHaigo] at 2018-9-24 23:03:08

hello!
Welcome to your next beauty destination!
“Art of Beauty” brand was the first one in the UAE. Despite the difference in cultural and linguistic barriers, as well as social norms of a new country, we are offering an ultimate approach for a beauty care as an essential part of individual health care. It`s one of the selected centers, that offers a wide range of face, hair, and body treatments for all- ladies, gentlemen, and children.
Site: https://nailsandhair.artofbeautycenter.ae
Phone: +971 55 173 6778
Location: 1017b, Al Wasl Road, Umm Suqeim 2, Manara Area - Dubai, UAE
By [BettyHaigo] at 2018-9-24 23:03:11


Post a Comment
呢称: 验证码: 
禁止笑脸转换 禁止UBB | 缩放输入框:6 5 | [Ctrl+Enter提交](1000个字符限制)