Java:解析XML数据的函数-不输出任何内容

Java:解析XML数据的函数-不输出任何内容,java,xml,Java,Xml,我对使用Java处理XML比较陌生,因此可能会有一些错误,但无论如何……我正在尝试解析以下XML数据: 我想使用一个函数来实现这一点,其中XML标记和节点列表的名称作为参数传入,并返回内容 谢谢 import java.io.*; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurati

我对使用Java处理XML比较陌生,因此可能会有一些错误,但无论如何……我正在尝试解析以下XML数据:

我想使用一个函数来实现这一点,其中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;
import org.xml.sax.SAXException;

public class Files {

    @SuppressWarnings("unused")

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

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

String author = "author";
String title = "title";
String genre = "genre";
String price = "price";
String publish = "publish_date";
String descr = "description";


    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");
    System.out.println("Number of books " + n.getLength());

    getElement(author, n);

        }

    private static void getElement(String elementName, NodeList n){

            for (int i = 0; i < n.getLength(); i++){
            Node showNode = n.item(i);

            Element showElement = (Element)showNode;

            System.out.println(elementName + ": " +
            showElement.getAttribute(elementName)

            );

        }

    }

    }
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;
导入org.xml.sax.SAXException;
公共类文件{
@抑制警告(“未使用”)
公共静态void main(字符串[]args)引发IOException、ParserConfiguration异常、SAXException{
字符串地址=“/home/leo/workspace/Test/Files/src/file.xml”;
字符串author=“author”;
字符串title=“title”;
String-genre=“genre”;
字符串price=“price”;
字符串publish=“publish\u date”;
String descr=“description”;
文件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(“book”);
System.out.println(“图书数量”+n.getLength());
getElement(作者,n);
}
私有静态void getElement(字符串elementName,节点列表n){
对于(int i=0;i
问题是:showElement.getAttribute(elementName)

您想要获取节点的值,但是getAttribute要获取节点的属性,您应该了解属性在XML中的含义

您可以获得如下值:

private static void getElement(String elementName, NodeList n){

    for (int i = 0; i < n.getLength(); i++){
        Node showNode = n.item(i);

        NodeList nl = showNode.getChildNodes();
        for(int j=0;j<nl.getLength();j++)
        {
            Node nd=nl.item(j);
            if(nd.getNodeName().equals(elementName))
            {
                 System.out.println(elementName + ":" + nd.getTextContent());
            }
        }
    }
}
private静态void getElement(String elementName,NodeList n){
对于(int i=0;i对于(int j=0;jfine,有什么问题吗?除了elementname的文本之外,什么都没有出现。您是否使用调试器逐步检查了代码?