Search code examples
javaxmltagselementparent

Java. Moving in a xml with the same tag name as child


The problem I have is that I have to work with an xml file the providers of the company I work for sent to me.
This would not be a problem if the xml was well constructed but it is not at all.

<catalog>
    <product>
        <ref>4780</ref>
             .
             .
             .
        <arrivals>
            <product>
                <image title="AMARILLO">AMA</image>
                <size>S/T </size>
            </product>
            <product>
                <image title="AZUL">AZUL</image>
                <size>S/T </size>
            </product>
        </arrivals>
    </product>
</catalog>

As you can see, the tag <product> have all the information of the product but there are more tags named <product> to distinguish when there are different colors.
This is the code I use to move in the xml.

doc = db.parse("filename.xml");
Element esproducte = (Element)doc.getElementsByTagName("product").item(0);

NodeList nArrv = esproducte.getElementsByTagName("arrivals");
Element eArrv = (Element) nArrv.item(0);
NodeList eProds = eArrv.getElementsByTagName("product");//THIS THING

for(int l=0; l<eProds.getLength(); l++)
{
Node ln = eProds.item(l);
if (ln.getNodeType() == Node.ELEMENT_NODE)
{
    Element le = (Element) ln;

    //COLORS / IMAGES / CONFIGS
    NodeList nimgcol = le.getElementsByTagName("image");
    Element eimgcol = (Element) nimgcol.item(0);
    System.out.println("Name of the color " + eimgcol.getTextContent());
}

What happens is that the print is reapeated more times it should and I think it's because of the parent <product>. I thought it shouldn't happen because where I wrote //THIS THING I take into account the fact that <product> is set in <arrivals>. But it is not working.
What should I modify in the code to move only 2 times in the for and not 3, which is what happen in this case?

Solution:

NodeList eProds = eArrv.getElementsByTagName("product");//THIS THING

to

NodeList eProds = eArrv.getChildNodes();//THIS THING

And the rest exactly the same. Works perfect.


Solution

  • getElementsByTagName give you all Tags with the name "product" that are inside that tag, including those "product" tags for colors. Try use getChildNodes and check the name of the Nodes instead