为什么解析器工作但在我尝试打印时返回空数据?



程序应该打印以前从.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();
}
}

名为"解析器"的分析器类

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>

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

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

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