先不加递归条件
最外层的循环就有问题,我都整蒙了,“北京”怎么打了2遍呢?
0[id=2 pid=1 value=北京] 1[id=2 pid=1 value=北京] 2[id=3 pid=1 value=上海] 3[id=4 pid=1 value=辽宁 ]
麻烦大家帮我看看,非常感谢!!!
===========================<!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"/>
<meta http-equiv="Content-Language" content="zh-cn"/>
<title>xml</title>
</head><body>
<div id="result"></div>
<script type="text/javascript">
function createDocument() {
if (window.ActiveXObject) {
var aVersions = [
"MSXML2.DOMDocument.6.0",
"MSXML2.DOMDocument.3.0",
"Microsoft.XmlDom"
]; for (var i = 0; i < aVersions.length; i++) {
try {
var oXmlDom = new ActiveXObject(aVersions[i]);
return oXmlDom;
} catch (oError) {
//Do nothing
}
}
throw new Error("MSXML is not installed.");
} else if (document.implementation && document.implementation.createDocument) {
Document.prototype.readyState = 0;
Document.prototype.onreadystatechange = null; Document.prototype._changeReadyState = function(iReady) {
this.readyState = iReady;
if (typeof this.onreadystatechange == "function") {
this.onreadystatechange();
}
}; Document.prototype._load = Document.prototype.load;
Document.prototype.load = function(sURL) {
this._changeReadyState(1);
this.load(sURL);
};
var oXmlDom = document.implementation.createDocument("", "", null);
oXmlDom.addEventListener("load", function() {
this._changeReadyState(4);
}, false);
return oXmlDom;
} else {
throw new Error("Your browser doesn't support an XML DOM object.");
}
}//end createDocument var oXmlDom = createDocument();
oXmlDom.onreadystatechange = function () {
if (oXmlDom.readyState == 4) {
var oRoot = oXmlDom.documentElement;
parseInfo(oRoot);
printList();
}
};
oXmlDom.validateOnParse = false;
oXmlDom.load("country.xml"); var list = new Array();
function addNode(id, pid, value) {
list[list.length] = new Node(id, pid, value);
} function Node(id, pid, value) {
this.id = id;
this.pid = pid;
this.value = value;
} function printList() {
var tmp = [];
for (var i = 0; i < list.length; i++) {
var node = list[i];
tmp[tmp.length] = i + "[id=" + node.id + " pid=" + node.pid + " value=" + node.value + "] ";
}
document.getElementById("result").innerHTML = tmp.join("");
} function parseInfo(root) {
var len = getChildNodesLength(root);
for (var i = 0; i < len; i++) {
var node = getNode(root.childNodes[i]);
var id = node.getAttribute("id");
var pid = node.getAttribute("pid");
var value = getXMLNodeText(node);
//北京打了2遍
alert("id=" + id + " pid=" + pid + " value=" + value);
addNode(id, pid, value); //先不加递归
/*if (hasChildren(node)) {
parseInfo(node);
}*/ }
} function hasChildren(node) {
return getChildNodesLength(node) > 0;
} function getChildNodesLength(parentNode) {
var oNodes = parentNode.childNodes;
var n = 0;
for (var i = 0, len = oNodes.length; i < len; i++) if (oNodes[i].nodeType == 1) n++;
return n;
} function getNode(node) {
if (node.nodeType == 1) {
return node;
} else {
var n = node.nextSibling;
while (n.nodeType != 1 && n.nextSibling != null) {
n = n.nextSibling;
}
return (n.nodeType == 1) ? n : false;
}
} function getXMLNodeText(node) {
return node.firstChild.nodeValue;
}
</script>
</body>
</html>========================
country.xml<?xml version='1.0' encoding='utf-8'?>
<root>
<!--<country id="1" pid="0">中国-->
<municipality id="2" pid="1">北京</municipality>
<municipality id="3" pid="1">上海</municipality>
<province id="4" pid="1">辽宁
<city id="5" pid="4">沈阳</city>
<city id="6" pid="4">大连</city>
</province>
<municipality id="7" pid="1">天津</municipality>
<!--</country>-->
</root>
最外层的循环就有问题,我都整蒙了,“北京”怎么打了2遍呢?
0[id=2 pid=1 value=北京] 1[id=2 pid=1 value=北京] 2[id=3 pid=1 value=上海] 3[id=4 pid=1 value=辽宁 ]
麻烦大家帮我看看,非常感谢!!!
===========================<!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"/>
<meta http-equiv="Content-Language" content="zh-cn"/>
<title>xml</title>
</head><body>
<div id="result"></div>
<script type="text/javascript">
function createDocument() {
if (window.ActiveXObject) {
var aVersions = [
"MSXML2.DOMDocument.6.0",
"MSXML2.DOMDocument.3.0",
"Microsoft.XmlDom"
]; for (var i = 0; i < aVersions.length; i++) {
try {
var oXmlDom = new ActiveXObject(aVersions[i]);
return oXmlDom;
} catch (oError) {
//Do nothing
}
}
throw new Error("MSXML is not installed.");
} else if (document.implementation && document.implementation.createDocument) {
Document.prototype.readyState = 0;
Document.prototype.onreadystatechange = null; Document.prototype._changeReadyState = function(iReady) {
this.readyState = iReady;
if (typeof this.onreadystatechange == "function") {
this.onreadystatechange();
}
}; Document.prototype._load = Document.prototype.load;
Document.prototype.load = function(sURL) {
this._changeReadyState(1);
this.load(sURL);
};
var oXmlDom = document.implementation.createDocument("", "", null);
oXmlDom.addEventListener("load", function() {
this._changeReadyState(4);
}, false);
return oXmlDom;
} else {
throw new Error("Your browser doesn't support an XML DOM object.");
}
}//end createDocument var oXmlDom = createDocument();
oXmlDom.onreadystatechange = function () {
if (oXmlDom.readyState == 4) {
var oRoot = oXmlDom.documentElement;
parseInfo(oRoot);
printList();
}
};
oXmlDom.validateOnParse = false;
oXmlDom.load("country.xml"); var list = new Array();
function addNode(id, pid, value) {
list[list.length] = new Node(id, pid, value);
} function Node(id, pid, value) {
this.id = id;
this.pid = pid;
this.value = value;
} function printList() {
var tmp = [];
for (var i = 0; i < list.length; i++) {
var node = list[i];
tmp[tmp.length] = i + "[id=" + node.id + " pid=" + node.pid + " value=" + node.value + "] ";
}
document.getElementById("result").innerHTML = tmp.join("");
} function parseInfo(root) {
var len = getChildNodesLength(root);
for (var i = 0; i < len; i++) {
var node = getNode(root.childNodes[i]);
var id = node.getAttribute("id");
var pid = node.getAttribute("pid");
var value = getXMLNodeText(node);
//北京打了2遍
alert("id=" + id + " pid=" + pid + " value=" + value);
addNode(id, pid, value); //先不加递归
/*if (hasChildren(node)) {
parseInfo(node);
}*/ }
} function hasChildren(node) {
return getChildNodesLength(node) > 0;
} function getChildNodesLength(parentNode) {
var oNodes = parentNode.childNodes;
var n = 0;
for (var i = 0, len = oNodes.length; i < len; i++) if (oNodes[i].nodeType == 1) n++;
return n;
} function getNode(node) {
if (node.nodeType == 1) {
return node;
} else {
var n = node.nextSibling;
while (n.nodeType != 1 && n.nextSibling != null) {
n = n.nextSibling;
}
return (n.nodeType == 1) ? n : false;
}
} function getXMLNodeText(node) {
return node.firstChild.nodeValue;
}
</script>
</body>
</html>========================
country.xml<?xml version='1.0' encoding='utf-8'?>
<root>
<!--<country id="1" pid="0">中国-->
<municipality id="2" pid="1">北京</municipality>
<municipality id="3" pid="1">上海</municipality>
<province id="4" pid="1">辽宁
<city id="5" pid="4">沈阳</city>
<city id="6" pid="4">大连</city>
</province>
<municipality id="7" pid="1">天津</municipality>
<!--</country>-->
</root>
<!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"/>
<meta http-equiv="Content-Language" content="zh-cn"/>
<title>xml</title>
</head><body>
<div id="result"></div>
<script type="text/javascript">
function createDocument() {
if (window.ActiveXObject) {
var aVersions = [
"MSXML2.DOMDocument.6.0",
"MSXML2.DOMDocument.3.0",
"Microsoft.XmlDom"
]; for (var i = 0; i < aVersions.length; i++) {
try {
var oXmlDom = new ActiveXObject(aVersions[i]);
return oXmlDom;
} catch (oError) {
//Do nothing
}
}
throw new Error("MSXML is not installed.");
} else if (document.implementation && document.implementation.createDocument) {
Document.prototype.readyState = 0;
Document.prototype.onreadystatechange = null; Document.prototype._changeReadyState = function(iReady) {
this.readyState = iReady;
if (typeof this.onreadystatechange == "function") {
this.onreadystatechange();
}
}; Document.prototype._load = Document.prototype.load;
Document.prototype.load = function(sURL) {
this._changeReadyState(1);
this.load(sURL);
};
var oXmlDom = document.implementation.createDocument("", "", null);
oXmlDom.addEventListener("load", function() {
this._changeReadyState(4);
}, false);
return oXmlDom;
} else {
throw new Error("Your browser doesn't support an XML DOM object.");
}
}//end createDocument
var oXmlDom = createDocument();
oXmlDom.onreadystatechange = function () {
if (oXmlDom.readyState == 4) {
var oRoot = oXmlDom.documentElement;
parseInfo(oRoot);
printList();
}
};
oXmlDom.validateOnParse = false;
oXmlDom.load("country.xml"); var list = new Array();
function addNode(id, pid, value) {
list[list.length] = new Node(id, pid, value);
} function Node(id, pid, value) {
this.id = id;
this.pid = pid;
this.value = value;
} function printList() {
var tmp = [];
for (var i = 0; i < list.length; i++) {
var node = list[i];
tmp[tmp.length] = i + "[id=" + node.id + " pid=" + node.pid + " value=" + node.value + "] ";
}
document.getElementById("result").innerHTML = tmp.join("");
} function parseInfo(root) {
var len = getChildNodesLength(root);
for (var i = 0; i < root.childNodes.length; i++) {
if(root.childNodes[i].nodeType == 1){
var node = getNode(root.childNodes.item(i));
var id = node.getAttribute("id");
var pid = node.getAttribute("pid");
var value = getXMLNodeText(node);
//北京打了2遍
alert("id=" + id + " pid=" + pid + " value=" + value);
addNode(id, pid, value);
} //先不加递归
/*if (hasChildren(node)) {
parseInfo(node);
}*/ }
} function hasChildren(node) {
return getChildNodesLength(node) > 0;
} function getChildNodesLength(parentNode) {
var oNodes = parentNode.childNodes;
var n = 0;
for (var i = 0, len = oNodes.length; i < len; i++) if (oNodes[i].nodeType == 1) n++;
return n;
} function getNode(node) {
if (node.nodeType == 1) {
return node;
} else {
var n = node.nextSibling;
while (n.nodeType != 1 && n.nextSibling != null) {
n = n.nextSibling;
}
return (n.nodeType == 1) ? n : false;
}
} function getXMLNodeText(node) {
return node.firstChild.nodeValue;
}
</script>
</body>
</html>
var len = getChildNodesLength(root);//这句话可要可不要
for (var i = 0; i < root.childNodes.length; i++) {
if(root.childNodes[i].nodeType == 1){//把判断加在这里
var node = getNode(root.childNodes.item(i));
var id = node.getAttribute("id");
var pid = node.getAttribute("pid");
var value = getXMLNodeText(node);
//北京打了2遍
alert("id=" + id + " pid=" + pid + " value=" + value);
addNode(id, pid, value);
} //先不加递归
/*if (hasChildren(node)) {
parseInfo(node);
}*/ }
}
<!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"/>
<meta http-equiv="Content-Language" content="zh-cn"/>
<title>xml</title>
</head><body>
<div id="result"></div>
<script type="text/javascript">
function createDocument() {
if (window.ActiveXObject) {
var aVersions = [
"MSXML2.DOMDocument.6.0",
"MSXML2.DOMDocument.3.0",
"Microsoft.XmlDom"
]; for (var i = 0; i < aVersions.length; i++) {
try {
var oXmlDom = new ActiveXObject(aVersions[i]);
return oXmlDom;
} catch (oError) {
//Do nothing
}
}
throw new Error("MSXML is not installed.");
} else if (document.implementation && document.implementation.createDocument) {
Document.prototype.readyState = 0;
Document.prototype.onreadystatechange = null; Document.prototype._changeReadyState = function(iReady) {
this.readyState = iReady;
if (typeof this.onreadystatechange == "function") {
this.onreadystatechange();
}
}; Document.prototype._load = Document.prototype.load;
Document.prototype.load = function(sURL) {
this._changeReadyState(1);
this.load(sURL);
};
var oXmlDom = document.implementation.createDocument("", "", null);
oXmlDom.addEventListener("load", function() {
this._changeReadyState(4);
}, false);
return oXmlDom;
} else {
throw new Error("Your browser doesn't support an XML DOM object.");
}
}//end createDocument var oXmlDom = createDocument();
oXmlDom.onreadystatechange = function () {
if (oXmlDom.readyState == 4) {
var oRoot = oXmlDom.documentElement;
parseInfo(oRoot);
printList();
}
};
oXmlDom.validateOnParse = false;
oXmlDom.load("country.xml"); var list = new Array();
function addNode(id, pid, value) {
list[list.length] = new Node(id, pid, value);
} function Node(id, pid, value) {
this.id = id;
this.pid = pid;
this.value = value;
} function printList() {
var tmp = [];
for (var i = 0; i < list.length; i++) {
var node = list[i];
tmp[tmp.length] = i + "[id=" + node.id + " pid=" + node.pid + " value=" + node.value + "] ";
}
document.getElementById("result").innerHTML = tmp.join("");
} function parseInfo(root) {
var len = root.childNodes.length;
for (var i = 0; i < len; i++) {
var node = root.childNodes[i];
if(node.nodeType != 1) continue;
var id = node.getAttribute("id");
var pid = node.getAttribute("pid");
var value = getXMLNodeText(node);
//北京打了2遍
alert("id=" + id + " pid=" + pid + " value=" + value);
addNode(id, pid, value); //先不加递归
/*if (hasChildren(node)) {
parseInfo(node);
}*/
}
} function hasChildren(node) {
return getChildNodesLength(node) > 0;
} function getChildNodesLength(parentNode) {
var oNodes = parentNode.childNodes;
var n = 0;
for (var i = 0, len = oNodes.length; i < len; i++) if (oNodes[i].nodeType == 1) n++;
return n;
} function getNode(node) {
if (node.nodeType == 1) {
return node;
} else {
var n = node.nextSibling;
while (n != null && n.nodeType != 1) {
n = n.nextSibling;
}
return (n.nodeType == 1) ? n : null;
}
} function getXMLNodeText(node) {
return node.firstChild.nodeValue;
}
</script>
</body>
</html>