以下是我的xml文件
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<book>
<bookID>1111</bookID>
</book>
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<book>
<bookID>54655</bookID>
</book>
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<book>
<bookID>5556</bookID>
</book>
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="web" cover="paperback">
<book>
<bookID>1111</bookID>
</book>
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
我需要显示书籍的“标题”。
下面用于显示数据的asp代码
<%
'' #Load XML
Set xml= Server.CreateObject("Msxml2.DOMDocument.3.0")
xml.async = False
xml.load (Server.MapPath("test.xml"))
if xml.parseError.errorcode<>0 then
response.write "error handling code" &xml.parseError.errorcode
else
Set objLst= xml.getElementsByTagName("bookstore").item(0).getElementsByTagName("book")
TotalBooks = (objLst.Length)-1
For i=0 to eval(TotalBooks)
response.write xml.getElementsByTagName("bookstore").item(0).getElementsByTagName("book").item(i).getElementsByTagName("title").item(0).text&"<br/>"
Next
end if
%>
但是“objLst.length”显示了<book>
因此导致错误
代码仅显示第一个节点书名。它不会进入第二个节点。我修好了吗?
i need out put like below
Everyday Italian
Harry Potter
XQuery Kick Start
Learning XML
答案 0 :(得分:2)
如果您不能将System.Xml.Linq用作@sonjz建议,那么您可以使用SelectNodes和XPath表达式
set nodes = xml.documentElement.selectNodes("//book/title")
for each node in nodes
response.write node.text & "<br/>"
next
答案 1 :(得分:-1)
使用System.Xml.Linq,这将是最简单的。
将其加载到XElement并使用Linq进行查询。
XDocument test = XDocument.Load(xmlFilename); // load from string if you want
XElement cooking = test.Element("bookstore")
.Descendants("book")
.Where(e => e.Attribute("category" => "cooking")
.FirstOrDefault();