处理XML文档的属性和方法(具体介绍自己找):
属性:
childNodes
firstChild
lastChild
nextSibling
nodeValue
parentNode
previousSibling
方法:
getElementById(id) (document)
getElementsByTagName(name)
hasChildNodes()
getAttribute(name)
需要注意的是XML的层次关系,返回的是数组时要便利处理,如果只返回一个,则要在后面加[0]来得到这唯一的一个值。
例如在parseXML.html和parseXML.xml中,如果只有一个<north></north>对,那么
var northNode = xmlDoc.getElementsByTagName("north")[0];
就可以了。但我给他加了个<north></north>对,就要通过循环来得到两个north对中的全部内容了。(对于没有JavaScript调试器的机器,用alert()来调试也很管用。)
parseXML.xml<?xml version="1.0" encoding="UTF-8"?>
<states>
<north>
<state>Minnesota</state>
<state>Iowa</state>
<state>North Dakota</state>
</north>
<north> <state>1</state> <state>2</state> <state>3</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>
parseXML.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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"); var out = "Northern States";
var s=""
for(var i = 0; i < northNode.length; i++) { var states = northNode[i].getElementsByTagName("state"); for(var j=0; j< states.length; j++) s = s + states[j].childNodes[0].nodeValue + "\n"; }
alert(s);
outputList("Northern States", s);
}
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>
Trackback: http://tb.donews.net/TrackBack.aspx?PostId=1189711