非递归不忍直视
<script type=
"text/javascript" language=
"javascript">
function btnTest_Click() {
try {
var xmlDoc = new ActiveXObject(
"Microsoft.XMLDOM")
} catch (e) { alert(e
.message)
try {
xmlDoc
.async = false
xmlDoc
.load(
"ch9_books.xml")
}
catch (e) { alert(e
.message)
var nodes = xmlDoc
document
.write(
"<ul>节点类型(" + nodes
.nodeType +
")节点名(" + nodes
.nodeName +
")值(" + nodes
.nodeValue +
")")
document
.write(
"<ul>节点类型(" + nodes
.childNodes[
0]
.nodeType +
")节点名(" + nodes
.childNodes[
0]
.nodeName +
")值(" + nodes
.childNodes[
0]
.nodeValue +
")</ul>")
document
.write(
"<ul>节点类型(" + nodes
.childNodes[
1]
.nodeType +
")节点名(" + nodes
.childNodes[
1]
.nodeName +
")值(" + nodes
.childNodes[
1]
.nodeValue +
")</ul>")
var
x = xmlDoc
.documentElement
document
.write(
"<ul>节点类型(" +
x.nodeType +
")节点名(" +
x.nodeName +
")值(" +
x.nodeValue +
")")
var
y =
x.childNodes
for (i =
0
document
.write(
"<ul>节点类型(" +
y[i]
.nodeType +
")节点名(" +
y[i]
.nodeName +
")值(" +
y[i]
.nodeValue +
")</ul>")
if (
y[i]
.attributes) {
for (b =
0
document
.write(
"<ul>节点类型(" +
y[i]
.attributes[b]
.nodeType +
")节点名(" +
y[i]
.attributes[b]
.nodeName +
")值(" +
y[i]
.attributes[b]
.nodeValue +
")")
document
.write(
"</ul>")
}
}
document
.write(
"<ul>")
for (
z =
0
document
.write(
"<ul>节点类型(" +
y[i]
.childNodes[
z]
.nodeType +
")节点名(" +
y[i]
.childNodes[
z]
.nodeName +
")值(" +
y[i]
.childNodes[
z]
.nodeValue +
")</ul>")
if (
y[i]
.childNodes[
z]
.attributes) {
for (b =
0
document
.write(
"<ul>节点类型(" +
y[i]
.childNodes[
z]
.attributes[b]
.nodeType +
")节点名(" +
y[i]
.childNodes[
z]
.attributes[b]
.nodeName +
")值(" +
y[i]
.childNodes[
z]
.attributes[b]
.nodeValue +
")</ul>")
}
}
if (
y[i]
.childNodes[
z]
.childNodes) {
document
.write(
"<ul>")
for (a =
0
document
.write(
"<ul>节点类型(" +
y[i]
.childNodes[
z]
.childNodes[a]
.nodeType +
")节点名(" +
y[i]
.childNodes[
z]
.childNodes[a]
.nodeName +
")值(" +
y[i]
.childNodes[
z]
.childNodes[a]
.nodeValue +
")")
}
}
document
.write(
"</ul></ul>")
}
document
.write(
"</ul>")
}
document
.write(
"</ul></ul>")
}
</script>
递归的 注意变量生存周期
<script type=
"text/javascript" language=
"javascript">
function output(nodes) {
if (nodes
.childNodes) {
for (var
z =
0
document
.write(
"<ul>节点类型(" + nodes
.childNodes[
z]
.nodeType +
")节点名(" + nodes
.childNodes[
z]
.nodeName +
")值(" +
nodes
.childNodes[
z]
.nodeValue +
")</ul>")
if (nodes
.childNodes[
z]
.attributes) {
for (var b =
0
document
.write(
"<ul>节点类型(" + nodes
.childNodes[
z]
.attributes[b]
.nodeType +
")节点名(" +
nodes
.childNodes[
z]
.attributes[b]
.nodeName +
")值(" + nodes
.childNodes[
z]
.attributes[b]
.nodeValue +
")</ul>")
}
}
document
.write(
"<ul>")
output(nodes
.childNodes[
z])
document
.write(
"</ul>")
}
}
else return
}
function btnTest_Click() {
try { var xmlDoc = new ActiveXObject(
"Microsoft.XMLDOM")
catch (e) { alert(e
.message)
try { xmlDoc
.async = false
catch (e) { alert(e
.message)
var nodes = xmlDoc
document
.write(
"<ul>节点类型(" + nodes
.nodeType +
")节点名(" + nodes
.nodeName +
")值(" + nodes
.nodeValue +
")")
output(nodes)
}
</script>
输出结果略有差异
节点类型(
9) 节点名(
#document) 值(
null)
节点类型(
7) 节点名(
xml) 值(version
="1.0" encoding
="ISO-8859-1")
节点类型(
2)节点名(version)值(
1.0)
节点类型(
2)节点名(encoding)值(ISO
-8859-1) 主要这玩意多出来了,但是递归省了不少代码
节点类型(
1) 节点名(bookstore) 值(
null)
节点类型(
1) 节点名(book) 值(
null)
节点类型(
2) 节点名(category) 值(cooking)
节点类型(
1) 节点名(title) 值(
null)
节点类型(
2) 节点名(lang) 值(en)
节点类型(
3) 节点名(
#text) 值(Everyday Italian)
节点类型(
1) 节点名(author) 值(
null)
节点类型(
3) 节点名(
#text) 值(Giada De Laurentiis)
节点类型(
1) 节点名(year) 值(
null)
节点类型(
3) 节点名(
#text) 值(
2005)
节点类型(
1) 节点名(price) 值(
null)
节点类型(
3) 节点名(
#text) 值(
30.00)
节点类型(
1) 节点名(book) 值(
null)
节点类型(
2) 节点名(category) 值(children)
节点类型(
1) 节点名(title) 值(
null)
节点类型(
2) 节点名(lang) 值(en)
节点类型(
3) 节点名(
#text) 值(Harry Potter)
节点类型(
1) 节点名(author) 值(
null)
节点类型(
3) 节点名(
#text) 值(J K
. Rowling)
节点类型(
1) 节点名(year) 值(
null)
节点类型(
3) 节点名(
#text) 值(
2005)
节点类型(
1) 节点名(price) 值(
null)
节点类型(
3) 节点名(
#text) 值(
29.99)
节点类型(
1) 节点名(book) 值(
null)
节点类型(
2) 节点名(category) 值(web)
节点类型(
1) 节点名(title) 值(
null)
节点类型(
2) 节点名(lang) 值(en)
节点类型(
3) 节点名(
#text) 值(XQuery Kick Start)
节点类型(
1) 节点名(author) 值(
null)
节点类型(
3) 节点名(
#text) 值(James McGovern)
节点类型(
1) 节点名(author) 值(
null)
节点类型(
3) 节点名(
#text) 值(Per Bothner)
节点类型(
1) 节点名(author) 值(
null)
节点类型(
3) 节点名(
#text) 值(Kurt Cagle)
节点类型(
1) 节点名(author) 值(
null)
节点类型(
3) 节点名(
#text) 值(James Linn)
节点类型(
1) 节点名(author) 值(
null)
节点类型(
3) 节点名(
#text) 值(Vaidyanathan Nagarajan)
节点类型(
1) 节点名(year) 值(
null)
节点类型(
3) 节点名(
#text) 值(
2003)
节点类型(
1) 节点名(price) 值(
null)
节点类型(
3) 节点名(
#text) 值(
49.99)
节点类型(
1) 节点名(book) 值(
null)
节点类型(
2) 节点名(category) 值(web)
节点类型(
2) 节点名(cover) 值(paperback)
节点类型(
1) 节点名(title) 值(
null)
节点类型(
2) 节点名(lang) 值(en)
节点类型(
3) 节点名(
#text) 值(Learning
XML)
节点类型(
1) 节点名(author) 值(
null)
节点类型(
3) 节点名(
#text) 值(Erik T
. Ray)
节点类型(
1) 节点名(year) 值(
null)
节点类型(
3) 节点名(
#text) 值(
2003)
节点类型(
1) 节点名(price) 值(
null)
节点类型(
3) 节点名(
#text) 值(
39.95)
转载请注明原文地址: https://ju.6miu.com/read-7985.html