title: Convert JavaScript XML DOM Object to Object with certain structure
body: <p>I have a large XML DOM Object which was parsed via jQuery's <code>$.parseXML</code> function, returning a JavaScript DOM Object. With this XML I want to create an normal JavaScript Object with the following structure:</p>
  
  <pre><code>{
      name: 'my_tree',
      children: [
          { name: 'hello' },
          { name: 'wat' },
          {
              name: 'child_folder',
              children: [
                  {
                      name: 'child_folder',
                      children: [
                          { name: 'hello' },
                          { name: 'wat' }
                      ]
                  },
                  { name: 'hello' },
                  { name: 'wat' },
                  {
                      name: 'child_folder',
                      children: [
                          { name: 'hello' },
                          { name: 'wat' }
                      ]
                  }
              ]
          }
      ]
  }
  </code></pre>
  
  <p>The original XML would look something like this:</p>
  
  <pre><code>&lt;my_tree&gt;
      &lt;hello&gt;&lt;/hello&gt;
      &lt;wat&gt;&lt;/wat&gt;
      &lt;child_folder&gt;
          &lt;child_folder&gt;
              &lt;hello&gt;&lt;/hello&gt;
              &lt;wat&gt;&lt;/wat&gt;
          &lt;/child_folder&gt;
          &lt;hello&gt;&lt;/hello&gt;
          &lt;wat&gt;&lt;/wat&gt;
          &lt;child_folder&gt;
              &lt;hello&gt;&lt;/hello&gt;
              &lt;wat&gt;&lt;/wat&gt;
          &lt;/child_folder&gt;
      &lt;/child_folder&gt;
  &lt;/my_tree&gt;
  </code></pre>
  
  <p>I have tried something like the code below, but to no avail:</p>
  
  <pre><code>function xmlDomToObject(domObject) {
      var result = {children: []};
      for (var i = 0; i &lt; domObject.length; i++) {
          if (domObject[i].nodeName == "#text") {
              continue;
          }
  
          result['name'] = domObject[i].nodeName;
          result['children'].push(xmlDomToObject(domObject[i].childNodes));
      }
  
      return result;
  }
  
  var xmlObject = xmlDomToObject(xmlDomObject.childNodes);
  </code></pre>