从XML文件中调用特殊TAG的数据
作者: Aion 日期: 2006-03-26 09:48
显示在弹出窗口的数据反正是可以调用了
显示在哪儿其实都是随便的你要是喜欢可以用innerHTML显示在页面的某个地方
parseXML.html
CODE:
parse.xml
CODE:
显示在哪儿其实都是随便的你要是喜欢可以用innerHTML显示在页面的某个地方
parseXML.html
CODE:
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Parsing XML Response with the W3C DOM</title>
<script type="text/javascript">
var xmlHttp;
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", "parse.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 = "North 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 + "
-" + currentState.childNodes[0].nodeValue;
}
alert (out);
}
</script>
</head>
<body>
<strong>Process XML Document of U.S States </strong>
<input type="button" value="View All Listed States" onclick="startRequest('all');" />
<input type="button" value="View All Listed Northern States" onclick="startRequest('north');" />
</body>
</html>
parse.xml
CODE:
<?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>Oklahome</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>
评论: 0 |
引用: 0 |
阅读: 1296
发表评论
订阅
上一篇
返回
下一篇
标签:
Union (2008-02-25 17:20)
电信封锁BT/P2P的解决办法 (2008-01-06 10:58)
2007 fleeting time (2007-12-28 15:26)
LAMP架构的B2B系统 DIV+CSS (2007-12-09 16:35)
Nov.18.2007 Sacrifice (2007-11-18 02:48)
还让不让人说话了,伟大的ZF和D组织 (2007-08-30 23:04)
July.23.2007 开心的周末 (2007-07-24 00:06)
Canon IXUS 850IS 相机夜景斑点解决办法 (2007-07-09 22:58)
Jun.24.2007 入夏 (2007-06-24 00:46)