首页 > 解决方案 > 为什么解析器可以工作,但当我尝试打印它时返回空数据?

问题描述

该程序应该打印之前从 .xml 文件中解析的所有元素。它包括 3 个 java 类:主类、解析器和一些对象模型。

它似乎可以工作并且不会引发任何错误,但是当我尝试打印恢复的数据时,它似乎永远不会保存该数据(因此它打印“null”而不是字符串值)

我放了一些打印语句来查看错误在哪里,但看起来一切正常,我真的错过了一些小但重要的东西。

每个类的代码是:

主要的

public class Principal {

    public static void main(String[] args) {

        Parser parser = new Parser();

        try {
            parsearFich(parser);
        } catch (ParserConfigurationException | SAXException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

    private static void parsearFich(Parser parser) throws ParserConfigurationException, SAXException, IOException {
        parser.parseFicherosXml("biblioteca.xml");
        parser.parseDocument();
        parser.printLibro();

    }

}

名为“Parser”的解析器类

public class Parser {
    private Document dom = null;
    private ArrayList<Libro> libros = null;

    public Parser() {
        libros = new ArrayList<Libro>();
    }

    public void parseFicherosXml(String fichero) throws ParserConfigurationException, SAXException, IOException{
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();

        InputStream is = new FileInputStream(fichero);
        Reader rd = new InputStreamReader(is, "UTF-8");
        InputSource isrc = new InputSource(rd);
        isrc.setEncoding("UTF-8");

        dom = db.parse(isrc);
    }

    public void parseDocument() {

        Element doc = dom.getDocumentElement();
        NodeList nl = doc.getElementsByTagName("libro");

        if (nl != null && nl.getLength() > 0 ) {
            for (int i = 0; i< nl.getLength(); i++) {
                Element e1 = (Element)  nl.item(i);
                Libro l = getLibro(e1);
                libros.add(l);
            }
        }
    }


    private Libro getLibro(Element libro) {

        String editor = getTextValue(libro,"editor");
        String titulo = getTextValue(libro,"titulo");
        String pags = getTextValue(libro,"paginas");
        String anyo = getAributeValue(libro,"titulo");

        NodeList autores = libro.getElementsByTagName("nombre");
        String lista = "";

        for (int i =0; i < autores.getLength(); i++) {
            Element e = (Element) autores.item(i);
            lista = lista + " , " + e.getFirstChild().getNodeValue();
        }

        Libro l1 = new Libro();
        return l1;
    }

    private String getAributeValue(Element libro, String string) {

        String valor = null;
        NodeList nl = libro.getElementsByTagName(string);

        if (nl != null && nl.getLength()>0) {
            Element e1 = (Element)nl.item(0);
            valor = e1.getAttribute("anyo");
        }

        return valor;
    }

    private String getTextValue(Element libro, String string) {
        String valor = null;
        NodeList nl = libro.getElementsByTagName(string);

        if (nl != null && nl.getLength() > 0) {
            Element e1 = (Element) nl.item(0);
            valor = e1.getFirstChild().getNodeValue();
        }
        return valor;
    }

    public void printLibro() {
        Iterator<Libro> it = libros.iterator();
        StringBuilder sb = new StringBuilder();

        while(it.hasNext()) {
            Libro l = it.next();
            sb.append(l.toString() + "\n");

        }
        System.out.println(sb);
    }

}

和对象类(Libro)

public class Libro implements Serializable {

    private String titulo;
    private String autor;
    private String anyo;
    private String editor;
    private String paginas;
    private String id;

    public Libro(){

    }

    public Libro(String titulo,String autor,String anyo, String editor,String paginas,String id) {

        this.titulo = titulo;
        this.autor = autor;
        this.anyo = anyo;
        this.editor = editor;
        this.paginas = paginas;
        this.id = id;

    }

    public String getTitulo() {
        return titulo;
    }

    public void setTitulo(String titulo) {
        this.titulo = titulo;
    }

    public String getAutor() {
        return autor;
    }

    public void setAutor(String autor) {
        this.autor = autor;
    }

    public String getAnyo() {
        return anyo;
    }

    public void setAnyo(String anyo) {
        this.anyo = anyo;
    }

    public String getEditor() {
        return editor;
    }

    public void setEditor(String editor) {
        this.editor = editor;
    }

    public String getPaginas() {
        return paginas;
    }

    public void setPaginas(String paginas) {
        this.paginas = paginas;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
    public String toString(){
        String s = "Título: " + titulo + " Autor: " + autor + "Editor: " + editor + " Páginas: " + paginas + " Año publicación: " + anyo;
        return s;
    }
}

此外,我试图解析的 xml 文件是 biblioteca.xml

<?xml version="1.0" encoding="UTF-8"?>
<biblioteca>
    <libro>
        <titulo anyo="2008">Introduction to Linux</titulo>
        <autor>
            <nombre>Machtelt</nombre> 
            <nombre>Garrels</nombre>         
        </autor>
        <editor>O'Reilly</editor>
        <paginas>256</paginas>
    </libro>
    <libro>
        <titulo anyo="1991">El lenguaje de programación C</titulo>
        <autor>
            <nombre>Kernighan</nombre> 
            <nombre>Ritchie</nombre>
        </autor>
        <editor>Prentice Hall</editor>
        <paginas>294</paginas>
    </libro>
</biblioteca>

标签: javaeclipsexml-parsing

解决方案


您已经创建了 Libro 实例,但尚未在其中设置值,这就是它输出空值的原因。

您需要在getLibro()方法结束时执行此操作:

        Libro l1 = new Libro();
        l1.setAnyo(anyo);
        l1.setEditor(editor);
        l1.setPaginas(pags);
        l1.setTitulo(titulo);
        return l1;

推荐阅读