0

我相信这是一个简单的问题,但我很难找出它是如何工作的。

那是 XML 文件(来自 www.w3schools.com):

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<bookstore>
    <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>

正如您所见,XQuery Kick Start 一书的作者不止一位。但我找不到合适数量的作者的方法。那是我的代码:

public static void main(String argv[]) throws ParserConfigurationException, SAXException, IOException {

    File fXmlFile = new File("\books.xml");        
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);

    doc.getDocumentElement().normalize();

    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

    NodeList nList = doc.getElementsByTagName("book");

    System.out.println("----------------------------");

    for (int temp = 0; temp < nList.getLength(); temp++) {

        Node nNode = nList.item(temp);

        System.out.println("\nCurrent Element :" + nNode.getNodeName());

        if (nNode.getNodeType() == Node.ELEMENT_NODE) {

            Element eElement = (Element) nNode;

            System.out.println("Category : " + eElement.getAttribute("category"));
            System.out.println("Title : " + eElement.getElementsByTagName("title").item(0).getTextContent());
            System.out.println("Author : " + eElement.getElementsByTagName("author").item(0).getTextContent());
            System.out.println("Year : " + eElement.getElementsByTagName("year").item(0).getTextContent());
            System.out.println("Price : " + eElement.getElementsByTagName("price").item(0).getTextContent()); 
        }
    }

但结果我只会得到一位作者:

Root element :bookstore
----------------------------

Current Element :book
Categoria do Livro : cooking
Titulo : Everyday Italian
Autor : Giada De Laurentiis
Ano : 2005
Price : 30.00

Current Element :book
Categoria do Livro : children
Titulo : Harry Potter
Autor : J K. Rowling
Ano : 2005
Price : 29.99

Current Element :book
Categoria do Livro : web
Titulo : XQuery Kick Start
Autor : James McGovern
Ano : 2003
Price : 49.99

Current Element :book
Categoria do Livro : web
Titulo : Learning XML
Autor : Erik T. Ray
Ano : 2003
Price : 39.95

有谁知道获得正确数量元素的好方法? 很抱歉这个长问题,我不知道如何表达自己,所以我不得不在这里粘贴 *我是 DOM 新手*

4

1 回答 1

0

你总是得到第一作者,因为你正在检索节点列表的第一项

getElementsByTagName("author").item(0)

尝试迭代它们,因为可能不止一个

for (int i = 0; i < eElement.getElementsByTagName("author").getLength(); i++)
     System.out.println("Author : " + 
           eElement.getElementsByTagName("author").item(i).getTextContent());
于 2013-09-05T21:56:58.373 回答