getElementsByTagName devuelve 2 veces más elementos de los que realmente existen

I have parsing XML documents with Java 1.7. When I run the function document.getElementByTagName("Product"), I get a number of 6000, but if I open it with Notepad++, I see only 3000 entries.

I have problem with looping through all elements with the wrong number of entries.

preguntado el 31 de julio de 12 a las 14:07

give a sample xml and a sample code of how you parse it. -

Why is there a problem looping over the returned nodelist? -

Reduce your file to just a single product element and see what happens. That's a general strategy: when in doubt, simplify. -

reducing the file to a single product helped a lot. -

1 Respuestas

I solved the problem. The problem was with the XML file. The parent Node had the same name as one of the Children nodes. for example.

> <products>
>          <product>
>                  <name>Blbalbal</name>
>                  <size>14</size>
>                  <cost>2000 $</cost>
>                  <link>
>                        <product>www.somthing.com</product>
>                  </link>
>          </product>
>          <product>
>                  <name>Product2</name>
>                  <size>123</size>
>                  <cost>2 $</cost>
>                  <link>
>                        <product>www.somthing2.com</product>
>                  </link>
>          </product>
> </products>

Thats why it, each entry counted twice

Respondido 01 ago 12, 14:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.