title: Parse metadata attributes of XML file in java
body: <p>I am trying to parse XML file which is of the form.</p>
  
  <pre><code>&lt;parent tag&gt;
      &lt;child tag&gt;  
          &lt;element key="property1"&gt;value&lt;/element&gt; 
          &lt;element key="property2"&gt;value&lt;/element&gt;
      &lt;/child tag&gt;
  &lt;/parent tag&gt;
  </code></pre>
  
  <p>How can I get the value of the <code>element</code> tag having <code>property1</code>? My code is as follows.</p>
  
  <pre><code>public static ArrayList&lt;String&gt; parseXML(URL url_str,URLConnection conn_str,String root_tag,String child_tag) throws ParserConfigurationException, SAXException, IOException 
  { 
      String s = null;
      ArrayList &lt;String&gt; List_value=new ArrayList&lt;String&gt;();       
      DocumentBuilderFactory dbF = DocumentBuilderFactory.newInstance();
      DocumentBuilder dBuilder = dbF.newDocumentBuilder();
      Document doc = dBuilder.parse(conn_str.getInputStream());
      doc.getDocumentElement().normalize();
      System.out.println("Root : "+doc.getDocumentElement());
      System.out.println("****************");
      NodeList nList= doc.getElementsByTagName(root_tag);
      System.out.println("****************");
  
      for (int i = 0; i &lt; nList.getLength(); i++) {
           Node node = nList.item(i);
           if (node.getNodeType() == Node.ELEMENT_NODE) {
               Element element = (Element) node;
               NodeList nodelist1 = element.getElementsByTagName(child_tag);
               for (int i1 = 0; i1 &lt; nodelist1.getLength(); i1++) 
               {
                   Element element1 = (Element) nodelist1.item(i1);
                   NodeList fstNm = element1.getChildNodes();
                   s=fstNm.item(0).getNodeValue();
  
                  List_value.add(s);
              }
              for(int c=0;c&lt;List_value.size();c++)
              {
                      System.out.println(List_value.get(c));
              }
  
          }
  
      }
      return List_value;
  }
  </code></pre>
  
  <p>I am using DOM parser for the same. Kindly help.</p>
  
  <p>Thank you.</p>