Because getChildNodes gives me an unexpected result


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 ();


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

        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>

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

1 answer


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