title: How can i get the siblings of an XML element in Java (using the DOM parser)?
body: <p>I would like to get the siblings of an XML element in Java.</p>
  
  <p>The xml file is as follows:</p>
  
  <pre><code>  &lt;parent&gt;
      &lt;child1&gt; value 1 &lt;/child1&gt;
      &lt;child2&gt; value 2 &lt;/child2&gt;
      &lt;child3&gt; value 3 &lt;/child3&gt;
    &lt;/parent&gt;
  </code></pre>
  
  <p>My code in JAVA with DOM parser  is as follows:</p>
  
  <pre><code> package dom_stack;
  
   import java.io.File;
   import java.io.IOException;
   import javax.xml.parsers.DocumentBuilderFactory;
   import javax.xml.parsers.ParserConfigurationException;
   import org.w3c.dom.Document;
   import org.w3c.dom.NodeList;
   import org.xml.sax.SAXException;
  
  
   public class DOM_stack {
  
  
       public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
  
  
        File file = new File("root.xml");
        if (file.exists()){
              DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  
              Document doc;
               doc = dbf.newDocumentBuilder().parse("root.xml");
  
  
              NodeList elemNodeList = doc.getElementsByTagName("child1");                 
  
              for(int j=0; j&lt;elemNodeList.getLength(); j++) {
  
                  System.out.println("The node's value that you are looking now is : " +  elemNodeList.item(j).getTextContent());
                  System.out.println(" Get the Name of the Next Sibling " + elemNodeList.item(j).getNextSibling().getNodeName());
                  System.out.println(" Get the Value of the Next Sibling " + elemNodeList.item(j).getNextSibling().getNodeValue());
  
              }
  
  
          }//if file exists
       }
      }
  </code></pre>
  
  <p>Unfortunately the result is:</p>
  
  <pre><code>    run:
      The node's value that you are looking now is :  value 1 
       Get the Name of the Next Sibling #text
       Get the Value of the Next Sibling 
  </code></pre>
  
  <p>and it should be:</p>
  
  <pre><code>    run:
      The node's value that you are looking now is :  value 1 
       Get the Name of the Next Sibling child2
       Get the Value of the Next Sibling value2
  </code></pre>
  
  <p>So, how can i get the desirable output?</p>
  
  <p>thanks, in advance</p>