试图用Java解析XML数据-获取错误:“0”;对于类型NodeList";未定义getNodeType()方法;

试图用Java解析XML数据-获取错误:“0”;对于类型NodeList";未定义getNodeType()方法;,java,xml,Java,Xml,我刚开始学习如何在Java中处理/解析XML数据。在我的for循环之后的一行中,我得到一个错误,“类型NodeList的getNodeType()方法未定义”,该错误包含: 如果(n.getNodeType()==Node.ELEMENT\u Node){ 错误类型似乎是我忘了导入某些内容,但我相信我已经获得了所有信息。我正在使用microsoft的XML示例,链接如下: 提前谢谢 import java.io.*; import javax.xml.parsers.DocumentBuild

我刚开始学习如何在Java中处理/解析XML数据。在我的for循环之后的一行中,我得到一个错误,“类型NodeList的getNodeType()方法未定义”,该错误包含:

如果(n.getNodeType()==Node.ELEMENT\u Node){

错误类型似乎是我忘了导入某些内容,但我相信我已经获得了所有信息。我正在使用microsoft的XML示例,链接如下:

提前谢谢

import java.io.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;

public class Files {

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

    String address = "/home/leo/workspace/Test/Files/src/file.xml";

    File xmlFile = new File(address);

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = factory.newDocumentBuilder();
    Document doc = dBuilder.parse(xmlFile);

    doc.getDocumentElement().normalize();

    System.out.println(doc.getDocumentElement().getNodeName());

    NodeList n = doc.getElementsByTagName("book id");

    for (int temp = 0; temp < n.getLength(); temp++){
        System.out.println(n.item(temp));

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

        Element e = (Element) n;

        System.out.println("author : " + e.getAttribute("author"));
        System.out.println("title : " + e.getAttribute("title") );
        System.out.println("genre : " + e.getAttribute("genre"));
        System.out.println("price : " + e.getAttribute("price"));
        System.out.println("publish_date : " + e.getAttribute("publish_date"));
        System.out.println("description : " + e.getAttribute("description"));

             }
        }

    }   

}
import java.io.*;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入javax.xml.parsers.parserConfiguration异常;
导入org.w3c.dom.Document;
导入org.w3c.dom.Node;
导入org.w3c.dom.NodeList;
导入org.w3c.dom.Element;
公共类文件{
公共静态void main(字符串[]args)引发IOException、ParserConfiguration异常{
字符串地址=“/home/leo/workspace/Test/Files/src/file.xml”;
文件xmlFile=新文件(地址);
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=factory.newDocumentBuilder();
Document doc=dBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();
System.out.println(doc.getDocumentElement().getNodeName());
NodeList n=doc.getElementsByTagName(“图书id”);
对于(int-temp=0;temp
您正在对
节点列表
对象(n)调用
getNodeType()

您需要在
节点
对象上调用此函数。示例:

n.item(temp).getNodeType();

谢谢,这修复了错误。正如我之前所说的,我对Java中的XML解析是新手,我只是在学习在线代码/教程。我目前只打印“目录”如果你,或者其他人可以给我指出正确的方向,我们会很感激。@ USE2411290你应该发布关于这个问题的另一个问题,因为这是关于GETNoDeType()函数的。如果你能用的话,请考虑接受这个解决方案。谢谢!