title: find whether an element exists by a particular tag name in XML
body: <p>I have an XML file where some sub tags (child node elements) are optional.
  e.g.</p>
  
  <pre><code>&lt;part&gt;
     &lt;note&gt;
         &lt;/rest&gt;
     &lt;/note&gt;
  
     &lt;note&gt;
         &lt;pitch&gt;&lt;/pitch&gt;
     &lt;/note&gt;
  
     &lt;note&gt;
         &lt;pitch&gt;&lt;/pitch&gt;
     &lt;/note&gt;
  &lt;/part&gt;
  </code></pre>
  
  <p>But when I read the XML files by tags, it throws a NullPointerException - since some sub-tags are optional (e.g. rest and pitch in above example). How can I filter this out? I couldn't come across any methods to find whether an element exists by a particular tag name. Even if I have a condition to check whether getElementsByTagName("tag-name") method not returns NULL - still it goes in the condition body and obviously throw the exception.
  How may I resolve this?</p>
  
  <p>The java code is:</p>
  
  <pre><code>if(fstelm_Note.getElementsByTagName("rest")!=null){
      if(fstelm_Note.getElementsByTagName("rest")==null){
          break;
      }
      NodeList restElmLst = fstelm_Note.getElementsByTagName("rest");
      Element restElm = (Element)restElmLst.item(0);
      NodeList rest = restElm.getChildNodes();
  
      String restVal = ((Node)rest.item(0)).getNodeValue().toString();
  
  }else if(fstelm_Note.getElementsByTagName("note")!=null){
      if(fstelm_Note.getElementsByTagName("note")==null){
          break;
      }
  
      NodeList noteElmLst = fstelm_Note.getElementsByTagName("note");
      Element noteElm = (Element)noteElmLst.item(0);
  
      NodeList note = noteElm.getChildNodes();
      String noteVal = ((Node)note.item(0)).getNodeValue().toString();
  }
  </code></pre>
  
  <p>Any insight or suggestions are appreciated.
  Thanks in advance.</p>