title: How to parse the XML file and check each element having children or not in Java?
body: <p>I am having a XML file like below:</p>
  
  <pre><code>&lt;customers&gt;
      &lt;customer&gt;
          &lt;name&gt;XXX&lt;/name&gt;
          &lt;address&gt;Nagar&lt;/address&gt;
          &lt;number&gt;
               &lt;num1&gt;123&lt;/num1&gt;
               &lt;num2&gt;456&lt;/num2&gt;
          &lt;/number&gt;
      &lt;/customer&gt;
      &lt;customer&gt;
          &lt;name&gt;YYY&lt;/name&gt;
          &lt;address&gt;Nagar&lt;/address&gt;
          &lt;number&gt;
               &lt;num1&gt;789&lt;/num1&gt;
               &lt;num2&gt;012&lt;/num2&gt;
          &lt;/number&gt;
      &lt;/customer&gt;
  &lt;/customers&gt;
  </code></pre>
  
  <p>I need to parse the entire XML from top to bottom and get the child node information for each element.</p>
  
  <p>Excepted output:(node = children)</p>
  
  <pre><code>customers = customer,customer
  customer = name,address,number
  name = null
  address = null
  number = num1,num2
  num1 = null
  num2 = null
  customer = name,address,number
  name = null
  address = null
  number = num1,num2
  num1 = null
  num2 = null
  </code></pre>
  
  <p>I used DOM for this. Below given the code:</p>
  
  <pre><code>NodeList nodeList = doc.getElementsByTagName("*");
  for (int i = 0; i &lt; nodeList.getLength(); i++) {
  Node node = nodeList.item(i);
      if (node.getNodeType() == Node.ELEMENT_NODE) {
         System.out.println(node.getNodeName()+" = "+node.getChildNodes());
      }
  }
  </code></pre>
  
  <p>But I am getting output like this:</p>
  
  <pre><code>customers = [customers: null]
  customer = [customer: null]
  name = [name: null]
  address = [address: null]
  number = [number: null]
  num1 = [num1: null]
  num2 = [num2: null]
  customer = [customer: null]
  name = [name: null]
  address = [address: null]
  number = [number: null]
  num1 = [num1: null]
  num2 = [num2: null]
  </code></pre>
  
  <p>Could you please help me with this.</p>