title: Parsing a field that only some object contains
body: <pre><code>&lt;collection&gt;
      &lt;personRepresentaion&gt;
          &lt;theId&gt;1&lt;/theId&gt;
          &lt;name&gt;John&lt;/name&gt;
          &lt;height&gt;10&lt;/height&gt;
          &lt;width&gt;12&lt;/width&gt;
          &lt;relationships&gt;
              &lt;inputId&gt;1324&lt;/inputId&gt;
              &lt;outputId&gt;1325&lt;/outputId&gt;
          &lt;/relationships&gt;
      &lt;/personRepresentaion&gt;
      &lt;personRepresentaion&gt;
          &lt;theId&gt;2&lt;/theId&gt;
          &lt;name&gt;Bill&lt;/name&gt;
          &lt;height&gt;10&lt;/height&gt;
          &lt;width&gt;12&lt;/width&gt;
      &lt;/personRepresentaion&gt;
      &lt;personRepresentaion&gt;
          &lt;theId&gt;3&lt;/theId&gt;
          &lt;name&gt;Bob&lt;/name&gt;
          &lt;height&gt;10&lt;/height&gt;
          &lt;width&gt;12&lt;/width&gt;
          &lt;relationships&gt;
              &lt;inputId&gt;1324&lt;/inputId&gt;
              &lt;outputId&gt;1325&lt;/outputId&gt;
          &lt;/relationships&gt;
      &lt;/personRepresentaion&gt;
  &lt;/collection&gt;
  </code></pre>
  
  <p>CurrentlyI am this XML file, and I want to parse out the field &lt; outputId >, so I can use my function connectId(source, destination) to connect those 2 objects.</p>
  
  <p>The problem is that I am using a for loop like the following to display them on my page:</p>
  
  <pre><code>var x=xmlDoc.getElementsByTagName("personRepresentaion");
  
  for(i=0;i&lt;x.length;i++) { 
  connectId(x[i].getElementsByTagName("theId")[0].childNodes[0].nodeValue,
  x[i].getElementsByTagName("relationships")[0].getElementsByTagName("outputId")[0].childNodes[0].nodeValue);
  }
  </code></pre>
  
  <p>But <code>Bill</code> in the XML does not have the field <code>&lt; relationships &gt;</code>, so the loop just stops and exits when it gets to Bill. </p>
  
  <p>I need to first find only the persons that HAS &lt; relationships > field, then use a for loop, then call connectId() for those objects, how do I achieve this?</p>