title: How handle Cannot read property 'getAttribute' of undefined?
body: <p>How can I handle error Cannot read property 'getAttribute' of undefined. I'm using <code>DOMParser</code> to get <code>type</code> attribute of a xml tag. This xml tag sometimes can be <code>FirstTag</code> or other times can be <code>SecondTag</code>. I implement this that works fine with <code>FirstTag</code>, but when is <code>SecondTag</code> the error is shown in console.</p>
  
  <pre><code>var parser = new DOMParser();
  var content = '&lt;root&gt;' + content + '&lt;/root&gt;';
  xmlDoc = parser.parseFromString(content, "text/xml");
  
  if( xmlDoc.getElementsByTagName("FirstTag")[0] == 'undefined' ){
      var element = xmlDoc.getElementsByTagName("SecondTag")[0];
      var type = element.getAttribute("type");
  }else {
      var element = xmlDoc.getElementsByTagName("FirstTag")[0];
      var type = element.getAttribute("type");
  }
  </code></pre>