books.xml
1<books>
2<book>
3<title>ASP</title>
4<isbn>1-861003-38-2</isbn>
5<authors>
6<authors_name>Brian Francis</authors_name>
7<authors_name>Chris Ullman</authors_name>
8<authors_name>Lqf Lsh</authors_name>
9</authors>
10<description>ASP is a Porerful</description>
11<price us="$49.99"></price>
12</book>
13</books>
list.asp
1
2set objXML = server.createobject("microsoft.xmldom")
3objxml.load(server.mappath("books.xml"))
4strtitle=objxml.documentelement.firstchild.firstchild.text
5strISBN=objxml.documentelement.firstchild.childnodes(1).text
6strauthors_name1=objxml.documentelement.firstchild.childnodes(2).childnodes(0).text
7strauthors_name2=objxml.documentelement.firstchild.childnodes(2).childnodes(1).text
8strauthors_name3=objxml.documentelement.firstchild.childnodes(2).childnodes(2).text
9strdescription=objxml.documentelement.firstchild.childnodes(3).text
10
11response.write "title: "&strtitle&"
<br/>
1"
2response.write "ISBN: "&strISBN&"
<br/>
1"
2response.write "authors_name1: "&strauthors_name1&"
<br/>
1"
2response.write "authors_name2: "&strauthors_name2&"
<br/>
1"
2response.write "authors_name3: "&strauthors_name3&"
<br/>
1"
2response.write "description: "&strdescription&"
<br/>
1"
第一个问题:我怎样读取
1<price us="$49.99"></price>
的值,再不改变xml的情况下?
第二个问题:book下面有5个结点,在我不知道有多少个结点的情况下,怎样可以循环出5个结点所有的内容(不包括authors节点下的内容)?
第三个问题:接2问,包括authors节点下的内容该怎样写?
第四个问题: 我想用递归循环出这个xml的所有内容,也就是说在不知道有多少个结点的情况下,包括所有子节点全部显示出来。
第五个问题:如果这些问题解决,我另开贴加分,还会有后面的问题等您回答。
---------------------------------------------------------------
你用什么东东写ASP?
我觉得上面的问题如果先用VB写完后再改写成ASP代码,可能会简单很多。
一般都有:
objxml.documentelement.firstchild.count
试试看。
---------------------------------------------------------------
Answer 1: Response.Write(objxml.documentElement.firstChild.childNodes(4).getAttribute("US"))
Answer 2:
for each x in objxml.documentElement.firstChild.childNodes
Response.write(x.nodeName & "
1<br>")
2next
3
4
5Answer 3:
6
7for each x in objxml.documentElement.firstChild.childNodes(2).childNodes
8Response.write(x.childnodes(0).nodevalue & "<br>")
9next
10
11Anwser 4
12循环判断对节点 或 子节点 的 Attributes.length 和 childNodes.length 的个数 来显示
13程序比较烦琐 时间关系我不写了 xml是个好东西 不过lqflsh 还是通过asp.net来处理会简单多多地 :)
14
15\---------------------------------------------------------------
16
@ Language=VBScript
1
Response.CharSet="gb2312"
set objXML = server.createobject("microsoft.xmldom")
objxml.load(server.mappath("book.xml"))
'1
Dim node
set node = objxml.selectSingleNode("//price")
Response.Write node.attributes(0).value & "<br/>"
'2
Set node = objxml.selectSingleNode("//book")
for each node1 in node.childNodes
Response.Write node1.nodeName & ": " & node1.text
for i=1 to node1.attributes.length
Response.Write " " & node1.attributes(i-1).nodename & ":" & node1.attributes(i-1).value 26 "<br/>"
next
Response.Write "<br/>"
next
'3 and 4
Response.Write "<hr/>"
Set node = objxml.selectSingleNode("books")
for each node1 in node.childNodes
call showALlNode(node1)
next
function showALlNode(byref node2)
Response.Write node2.baseName & ": " & node2.nodeValue
if node2.nodetype =1 then
for i=1 to node2.attributes.length
Response.Write " " & node2.attributes(i-1).baseName & ":" & node2.attributes(i-1).value 26 "<br/>"
next
end if
Response.Write "<br/>"
if node2.hasChildNodes() Then
for each node3 in node2.childNodes
showALlNode(node3)
next
End if
End function
1
2
3
4
5
6
7
8\---------------------------------------------------------------
9
10问题1:
11set objXML = server.createobject("microsoft.xmldom")
12objxml.load(server.mappath("books.xml"))
13set node=objxml.selectsinglenode("books/book/price")
14price=node.attributes.getNamedItem("us").nodeValue
15
16问题2:
17set nodelist=objxml.doucmentElement.childNodes
18for each node in nodelist
19''循环该结点的直接子结点,若是文本结点就输出其text
20''元素结点归入问题3
21''属性结点忽略
22next
23
24
25问题3:
26和上面一样在循环中处理,根据结点类型判断处理
27
28问题4:
29已经没有疑问了.对元素结点递归调用2就好了.</br></br>