Blog Stats
Posts - 0
Articles - 6
Comments - 1
Trackbacks - 0
文章档案
2008年4月 (3)
2007年12月 (3)
相册
Ajax
asp.net 2.o
静
显示了服务器向浏览器返回的XML文档的内容
在浏览器上会生成具有两个按钮的
HTML
页面。点击第一个按钮,将从服务器加载
XML
文档,然后在警告框中显示列于文档中的所有州。点击第二个按钮也会从服务器加载
XML
文档,不过只在警告框中显示北部地区的各个州
。
ParseXML.htm完整代码:
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
>
<
title
>
Parsing XML Responses with the W3C DOM
</
title
>
<
script
type
="text/javascript"
>
var
xmlHttp;
var
requestType
=
""
;
function
createXMLHttpRequest()
{
if
(window.ActiveXObject)
{
xmlHttp
=
new
ActiveXObject(
"
Microsoft.XMLHTTP
"
);
}
else
if
(window.XMLHttpRequest)
{
xmlHttp
=
new
XMLHttpRequest();
}
}
function
startRequest(requestedList)
{
requestType
=
requestedList;
createXMLHttpRequest();
xmlHttp.onreadystatechange
=
handleStateChange;
xmlHttp.open(
"
GET
"
,
"
parseXML.xml
"
,
true
);
xmlHttp.send(
null
);
}
function
handleStateChange()
{
if
(xmlHttp.readyState
==
4
)
{
if
(xmlHttp.status
==
200
)
{
if
(requestType
==
"
north
"
)
{
listNorthStates();
}
else
if
(requestType
==
"
all
"
)
{
listAllStates();
}
}
}
}
function
listNorthStates()
{
var
xmlDoc
=
xmlHttp.responseXML;
var
northNode
=
xmlDoc.getElementsByTagName(
"
north
"
)[
0
];
var
out
=
"
Northern States
"
;
var
northStates
=
northNode.getElementsByTagName(
"
state
"
);
outputList(
"
Northern States
"
, northStates);
}
function
listAllStates()
{
var
xmlDoc
=
xmlHttp.responseXML;
var
allStates
=
xmlDoc.getElementsByTagName(
"
state
"
);
outputList(
"
All States in Document
"
, allStates);
}
function
outputList(title, states)
{
var
out
=
title;
var
currentState
=
null
;
for
(
var
i
=
0
; i
<
states.length; i
++
)
{
currentState
=
states[i];
out
=
out
+
"
\n-
"
+
currentState.childNodes[
0
].nodeValue;
}
alert(out);
}
</
script
>
</
head
>
<
body
>
<
h1
>
Process XML Document of U.S. States
</
h1
>
<
br
/><
br
/>
<
form
action
="#"
>
<
input
type
="button"
value
="View All Listed States"
onclick
="startRequest('all');"
/>
<
br
/><
br
/>
<
input
type
="button"
value
="View All Listed Northern States"
onclick
="startRequest('north');"
/>
</
form
>
</
body
>
</
html
>
ParseXML.xml内容:
<?
xml version="1.0" encoding="UTF-8"
?>
<
states
>
<
north
>
<
state
>
Minnesota
</
state
>
<
state
>
Iowa
</
state
>
<
state
>
North Dakota
</
state
>
</
north
>
<
south
>
<
state
>
Texas
</
state
>
<
state
>
Oklahoma
</
state
>
<
state
>
Louisiana
</
state
>
</
south
>
<
east
>
<
state
>
New York
</
state
>
<
state
>
North Carolina
</
state
>
<
state
>
Massachusetts
</
state
>
</
east
>
<
west
>
<
state
>
California
</
state
>
<
state
>
Oregon
</
state
>
<
state
>
Nevada
</
state
>
</
west
>
</
states
>
posted on 2008-04-12 12:23
学习者
阅读(84)
评论(0)
编辑
收藏
新用户注册
刷新评论列表
只有注册用户
登录
后才能发表评论。
网站导航:
博客园
IT新闻
Chat2DB
C++博客
博问
管理
Powered by:
.Text
and
ASP.NET
- Copyright © 学习者