I have an XML of the following format:
<Root>
<Delivery deliveryID="01">
<Product productID="001">
<name>test</name>
</Product>
..........
..........
..........
<Product productID="08">
<name>test</name>
</Product>
</Delivery>
<Delivery deliveryID="02">
<Product productID="001">
<name>test</name>
</Product>
..........
..........
..........
<Product productID="012">
<name>test</name>
</Product>
</Delivery>
</Root>
Is there any way to read only the child nodes of the first delivery element? Note that the number of child nodes inside the delivery elements could vary.
I was using the below code but it only works when I know the right number of elements that will appear inside the delivery tag. When the number of products inside first delivery changes, I have no way to find it
NodeList nList1 = doc.getElementsByTagName("Delivery");
for (int i = 0; i < nList1.getLength(); i++) {
Node nNode = nList1.item(i);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Product : "+ eElement.getElementsByTagName("name").item(i).getTextContent());
}
}
My main issue is having unknown number of child node with same name as child nodes of other nodes.