title: How to Read XML Response using Java
body: <p>I am calling a web service and get the response as XML. Here is XML response,</p>
  
  <pre><code>&lt;Response&gt;
      &lt;NO&gt;1&lt;/NO&gt;
      &lt;NAME&gt;John Doe&lt;/NAME&gt;
  &lt;/Response&gt;
  &lt;Response&gt;
      &lt;NO&gt;2&lt;/NO&gt;
      &lt;NAME&gt;Jane Doe&lt;/NAME&gt;
  &lt;/Response&gt;
  </code></pre>
  
  <p>I am trying to get each data from each response. I need to get each value.</p>
  
  <p>Here is my Java code,</p>
  
  <pre><code>Document document = DocumentBuilderFactory
                      .newInstance()
                      .newDocumentBuilder()
                      .parse(new InputSource(new StringReader(response.toString())));
  
              NodeList nodeList = document.getElementsByTagName("Response");
              System.out.println(nodeList.getLength());
  
              for (int i = 0; i &lt; nodeList.getLength(); i++) {
                  Node node = nodeList.item(i);
  
                  if (node.getNodeType() == Node.ELEMENT_NODE) {
                      Element element = (Element) node;
  
                      NodeList id = (NodeList) element.getElementsByTagName("Response");
                      System.out.println(id);
                      NodeList nodeLists = element.getChildNodes();
  
                      for (int j = 0; j &lt; nodeLists.getLength(); j++) {
                          Node n = nodeLists.item(j);
  
                          if (n.getNodeType() == Node.ELEMENT_NODE) {
                              Element name = (Element) n;
                              System.out.println(name.getAttributes().getNamedItem("NO").getNodeValue());
                              System.out.println(name.getAttributes().getNamedItem("NAME").getNodeValue());
                          }
                      }
                  }
              }
  </code></pre>
  
  <p>But I can't get What I need. Nothing prints. </p>
  
  <p>I need to print each value like this,</p>
  
  <pre><code>1
  John Doe
  2
  Jane Doe
  </code></pre>
  
  <p>Basically, it needs to iterate over all <code>&lt;Response&gt;</code> Tags and give me the available data. But my code doesn't work.</p>
  
  <p>So anybody can help me? Greatly appreciate that.</p>