title: how to get element by attribute using dom parser in java
body: <p>I am not getting an idea how to get an element by its attribute. I tried something as below but getting error as the item(1) might change as the loop goes on. some field tags may be stripped off. so position might change. I want to get:</p>
  
  <pre><code>&lt;field name="Test/Content/Modified"&gt;Thu Jun 01 13:11:43 2014&lt;/field&gt;
  </code></pre>
  
  <p><strong>Xml:</strong></p>
  
  <pre><code> &lt;assets&gt;
          &lt;document path="some/path/1"&gt;
              &lt;metadata&gt;                    
                  &lt;field name="Test/Content/Date"&gt;2013-12-20&lt;/field&gt;
                  &lt;field name="Test/Content/Modified"&gt;Thu Jun 01 13:11:43 2014&lt;/field&gt;
                  &lt;field name="Test/Locale"&gt;en_US&lt;/field&gt;
                  &lt;field name="Test/Content/SubSolution"&gt;&lt;/field&gt;
              &lt;/metadata&gt;
          &lt;/document&gt;
          &lt;document path="some/path/2"&gt;
              &lt;metadata&gt;                
                  &lt;field name="Test/Content/Date"&gt;2013-12-20&lt;/field&gt;
                  &lt;field name="Test/Locale"&gt;en_US&lt;/field&gt;
                  &lt;field name="Test/Content/Modified"&gt;Thu Jun 01 13:11:43 2014&lt;/field&gt;
                  &lt;field name="Test/Content/SubSolution"&gt;&lt;/field&gt;
              &lt;/metadata&gt;
          &lt;/document&gt;
   &lt;assets&gt;
  </code></pre>
  
  <p><strong>java partial code:</strong></p>
  
  <pre><code>DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
  Document doc = dBuilder.parse("E:\\example.xml");
  
  List&lt;String&gt; list = new ArrayList&lt;&gt;();
  
  NodeList nList = doc.getElementsByTagName("document");
  for (int i = 0; i &lt; nList.getLength(); i++) {
      Node nNode = nList.item(i);
      if (nNode.getNodeType() == Node.ELEMENT_NODE) {
          Element eElement = (Element) nNode;
          Date date = new Date(eElement.getElementsByTagName("field").item(1).getTextContent());
          System.out.println(date);
          Date date1 = new Date(2014 - 1900, 06 - 1, 04);
          if (date.compareTo(date1) == 1) {
              list.add(eElement.getAttribute("path").trim());
          }
      }
  }
  </code></pre>