Because getChildNodes gives me an unexpected result

1

When I get the children of the root node (books) I should get the result 3 (book, title and author) and I get 9 when applying getchildnotes ();

books.xml

<?xml version="1.0" encoding="UTF-8"?>
<Libros>
 <Libro publicado_en="1840">
 <Titulo>El Capote</Titulo>
 <Autor>Nikolai Gogol</Autor>
 </Libro>
 <Libro publicado_en="2008">
 <Titulo>El Sanador de Caballos</Titulo>
 <Autor>Gonzalo Giner</Autor>
 </Libro>
 <Libro publicado_en="1981">
 <Titulo>El Nombre de la Rosa</Titulo>
 <Autor>Umberto Eco</Autor>
 </Libro>
 <Libro publicado_en="1982">
 <Titulo>El libro de la selva</Titulo>
 <Autor>Mariola</Autor>
 </Libro>
</Libros>

Metodos.java

        Node nodo; //Nodo del árbol DOM
        Node libros = doc.getFirstChild(); //Obtiene la raíz <libros>(doc es libros.xml)



        NodeList libro = libros.getChildNodes(); //Obtiene los hijos del raíz <libro>
        System.out.println(libro.getLength());

When I try the code and monitor it in the last line it appears as result 9

    
asked by victor96 23.11.2018 в 15:49
source

1 answer

2

Why libros.getChildNodes() returns 4 nodes Book ( ELEMENT_NODE ) plus 5 text nodes TEXT_NODE with the value of \n , in total there are 9.

Why I already answered this in your previous question:

  

Because I get Nullpointerexception when I save an attribute of a   node. See answer .

    
answered by 23.11.2018 / 19:44
source