When obtaining the children of the root node (books) I should obtain as a result 3 (book, title and author) and it gives me 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>
Methods.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 testing the code and monitoring it, the last line shows me as a result 9
Because it
libros.getChildNodes()
returns 4 Book(ELEMENT_NODE
) nodes plus 5 text nodesTEXT_NODE
with the value of\n
, a total of 9.I already answered the reason for this in your previous question: