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()); 
        }
    }

しかし、結果として、著者は 1 人しか得られません。

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

適切な数の要素を取得するための良い方法を知っている人はいますか? 長い質問で申し訳ありません。私は自分自身を表現する方法を知らなかったので、ここに貼り付けなければなりませんでした *I'm new to 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 に答える