【发布时间】:2015-03-20 12:26:33
【问题描述】:
我有这个 XML 文件。我想用 Javascript 解析它以仅获取根节点中的 Example1 和 Example2 。如果我使用getElementsByTagName('author'),我将输出所有作者值。我能做什么?
<?xml version='1.0'?>
<bookstore>
<author>Example1</author>
<author>Example2</author>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<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">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
【问题讨论】:
-
document.querySelectorAll("bookstore > author").
标签: javascript xml parsing