您的位置:首页 > Web前端 > JavaScript

Javascript、ASP解析(远程)XML

2006-07-06 09:21 411 查看
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

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


<script language=javascript>
var xmlDoc;
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("/test/testxml/t1.xml");
document.write(xmlDoc.getElementsByTagName("title")[0].firstChild.nodeValue);
document.write(xmlDoc.getElementsByTagName("link")[0].firstChild.nodeValue);
document.write("<hr />");
document.write(xmlDoc.getElementsByTagName("title")[1].firstChild.nodeValue);
document.write(xmlDoc.getElementsByTagName("link")[1].firstChild.nodeValue);
</script>
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]

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

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

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

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

<script language=JavaScript>
var oXMLDoc = new ActiveXObject("MSXML");
oXMLDoc.url = "/test/testxml/t1.xml";
var oRoot=oXMLDoc.root;
if(oRoot.children != null)
{
document.write(oRoot.children.item(0).text);
document.write("<hr>");
document.write(oRoot.children.item(1).text);
}
</script>
[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的属性和方法定义如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Methods and properties of Microsoft.XMLDOM</title>
<style type="text/css">
<!--
body {
margin-left: 0px;
margin-right: 0px;
margin-top: 0px;
font-family: verdana, helvetica, sans-serif;
font-size: 12px;
}
h1 {
color:#4274c2;
font-size: 22px;
margin-left: 0px;
margin-top: 25px;
font-family: monospace;
}
h2 {
color: #4274c2;
font-size: 18px;
margin-left: 0px;
font-family: monospace;
margin-bottom: 5px;
}
h3 {
color:#4274c2;
font-size: 16px;
margin-left: 10px;
font-family: monospace;
margin-bottom: 2px;
}
h4 {
color:#4274c2;
font-size: 12px;
margin-left: 20px;
font-family: monospace;
margin-bottom: 2px;
}
a {
color:#553333;
}
div.header a {
color:#eeeecc;
}
div.header {
color:#eeeecc;
}
div.footer {
margin-left: 6px;
}
pre.code b {
color:#000000;
}
pre.out b {
color:#000000;
}
pre.code {
background-color:#f6f9e3;
color:#10205f;
padding-left:10px;
padding-right:10px;
padding-top:5px;
padding-bottom:5px;
margin-top:2px;
border:1px solid #004080;
margin-left:20px;
}
pre.out {
background-color:#ebf6f9;
color:#000000;
padding-left:10px;
padding-right:10px;
padding-top:5px;
padding-bottom:2px;
margin-top:2px;
margin-left:20px;
border:1px solid #b09040;
}
pre.syn {
background-color:#efe9cf;
color:#000000;
padding-left:10px;
padding-right:10px;
padding-top:5px;
padding-bottom:2px;
margin-top:2px;
margin-left:20px;
border:1px solid #4090b0;
}
pre.syn b {
color:#000000;
}
pre {
padding: 0px;
margin-top: 6px;
margin-bottom: 14px;
margin-left: 20px;
}
div.quote {
margin-left:40px;
margin-right:40px;
border:1px dotted #e0e0c0;
background-color: #d5e5c5;
font-size:11px;
}
div.small {
padding-left:40px;
padding-top:10px;
font-size:9px;
}
div.txt {
width: 400px;
margin-bottom:9px;
margin-left:20px;
}
div.txt_wide {
margin-bottom:9px;
margin-left:20px;
}
img.img {
margin-left:20px;
}
body table tr td table {
margin-left:20px;
}
div.filename {
font-size:10px;
font-style:italic;
margin-left:20px;
}
ul {margin-top:1px;margin-bottom:9px;}
ol {margin-top:1px;margin-bottom:9px;}
li {margin-bottom: 2px}
-->
</style>
</head>
<body bgcolor="#eeeecc" text="#000000" onLoad="if (self != top) top.location = self.location;">
<table border=0 cellpadding=0 cellspacing=0 width=100% summary='layout table'>
<tr valign='top'><td><h1>Methods and properties of Microsoft.XMLDOM</h1> </td></tr>
<tr valign='top'>
<td>
<h2>Document</h2>
<h3>Properties</h3>
<div class='txt'>
<ul>
<li>async<br>
boolean: specifies whether asynchronous download of the document is permitted.
<li>doctype
<li>documentElement
<li>implementation
<li>ondataavailable [ie]
<li>onreadystateChange [ie]
<li>ontransformnode [ie]
<li>parseError [ie]
<li>preserveWhiteSpace [ie]
<li>readyState
<li>resolveExternals [ie]
<li>setProperty ) [ie]<br>
The following (2<sup>nd</sup> level) properties can be set: <ul>
<li>AllowDocumentFunction
<li>ForcedResync
<li>MaxXMLSize
<li>NewParser
<li>SelectionLanguage
<li>SelectionNamespace
<li>ServerHTTPRequest
</ul>
<p>
for example: <br><code>xmlDoc.setProperty("SelectionLanguage", "XPath");<br>
selection = xmlDoc.selectNodes("//Customer");</code>
<li>url [ie]
<li>validateOnParse [ie]
</ul>
</div>
<h3>Methods</h3>
<div class='txt'>
<ul>
<li>abort [ie]
<li>createAttribute
<li>createCDATASection (data )
<li>createComment (comment)
<li>createDocumentFragment (data )
<li>createElement (tagName)
<li>createEntityReference (name )
<li>createNode [ie] (type, name, nameSpaceURI)
<li>createProcessingInstruction (target, data)
<li>createTextNode (data)
<li>getElementsByTagName (tagName)
<li>load [ie] (url)
<li>loadXML [ie] (xml_string)
<li>nodeFromID [ie] (id_string)
<li>save [ie] (objTarget)
</ul>
</div>
<h2>Node</h2>
<h3>Properties</h3>
<div class='txt'>
<ul>
<li>attributes<br>
returns an array of objects. An object in this array has the <i>name</i> and <i>value</i> property.
<li>baseName [ie]
<li>childNodes
<li>dataType [ie]
<li>definition [ie]
<li>firstChild
<li>lastChild
<li>namespaceURI [ie]
<li>nodeName
<li>nodeType<br>
Can be <ul>
<li>1: Element
<li>2: Attribute
<li>3: Text
<li>4: CDATA Section
<li>5: Entity Reference
<li>6: Entity
<li>7: Processing Instruction
<li>8: Comment
<li>9: Document
<li>10: Document Type
<li>11: Document Fragment
<li>12: Notation
</ul>
<li>nodeTypedValue [ie]
<li>nodeTypeString [ie]
<li>nodeValue<br>
This property defines the content of a div. The following example uses nodeValue to write the seconds since the page was loaded:
<div><pre class=code>
<script type='text/javascript'>
var secs=0;
<a href='http://www.adp-gmbh.ch/web/js/objects/window/index.html#settimeout'>setTimeout</a>('write_seconds()', 1000); // 1 second
function write_seconds() {
var span;
if (document.getElementById) {
span = document.getElementById('time');
if (span && span.firstChild && span.firstChild.nodeType == 3) {
span.firstChild.nodeValue = secs;
secs++;
}
setTimeout('write_seconds()', 1000); // 1 second
}
}
</script>
<span id='time'>0</span>
</pre></div>
<li>ownerDocument
<li>parentNode
<li>parsed [ie]
<li>prefix
<li>previousSibling [ie]
<li>specified [ie]
<li>text [ie]
<li>xml [ie]
</ul>
</div>
<h3>Methods</h3>
<div class='txt'>
<ul>
<li>appendChild (tagName)
<li>cloneNode (deep )<br>
If <i>deep</i> is true, the children and children's children are cloned as well.
<li>hasChildNodes ()
<li>insertBefore (newChild, refChild)
<li>removeChild (child)
<li>replaceChild (newChild, oldChild)
<li>selectNodes [ie] (patternString)<br>
Returns a list of nodes.<br>
For example: <br><code> xmlDoc.setProperty("SelectionNamespaces", "xmlns:xsl='http://www.w3.org/1999/XSL/Transform'");<br>
xmlDoc.setProperty("SelectionLanguage", "XPath");<br>
objNodeList = xmlDoc.documentElement.selectNodes("//xsl:template"); </code>
<li>selectSingleNode [ie] (patternString)
<li>transformNode [ie] (stylesheet)
<li>transformNodeToObject [ie] (stylesheet, outputObject)
</ul>
</div>
<h2>Thanks</h2>
<div class='txt'>
Thanks to <b>Enrique A. Gamez</b> who notified me of a type on this page.
</div>
</td></tr></table>
</body></html>
[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

[/b]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: