title: Parse XML using Google Apps Script
body: <p>I'm having difficulty parsing the XML from a boardgamegeek query so that I may populate a google sheet with the data. Here's an example of the bgg xml:</p>
  
  <pre><code>&lt;boardgames termsofuse="http://boardgamegeek.com/xmlapi/termsofuse"&gt;
    &lt;boardgame objectid="423"&gt;
      &lt;yearpublished&gt;1995&lt;/yearpublished&gt;
      &lt;minplayers&gt;3&lt;/minplayers&gt;
      &lt;maxplayers&gt;6&lt;/maxplayers&gt;
      &lt;playingtime&gt;300&lt;/playingtime&gt;
      &lt;name primary="true" sortindex="1"&gt;1856&lt;/name&gt;
    &lt;/boardgame&gt;
  &lt;/boardgames&gt;
  </code></pre>
  
  <p>And here's the Google Apps Script I've written to parse it:</p>
  
  <pre><code>//get the data from boardgamegeek
    var url = 'http://www.boardgamegeek.com/xmlapi/boardgame/' + bggCode;
    var bggXml = UrlFetchApp.fetch(url).getContentText();
  
    var document = XmlService.parse(bggXml);
    var root = document.getRootElement();     
    var entries = new Array();
    entries = root.getChildren('boardgame');
  
    for (var x = 0; x &lt; entries.length; i++) {
      var name = entries[x].getAttribute('name').getValue();
      var yearpublished = entries[x].getAttribute('yearpublished').getValue();
      var minplayers = entries[x].getAttribute('minplayers').getValue();
      var maxplayers = entries[x].getAttribute('maxplayers').getValue();
    }
    //SpreadsheetApp.getActiveSheet().getRange(i+1,7).setValue(yearpublished);
    Logger.log(entries);
  </code></pre>
  
  <p>I'm currently getting an error in the for-loop caused by entries being NULL. If I comment the loop out and log what bggXml looks like, it looks just like the example above. However, logging variables further down I get the following:</p>
  
  <pre><code>document =&gt; [Document:  No DOCTYPE declaration, Root is [Element: &lt;boardgames/&gt;]]
  root =&gt; [Element: &lt;boardgames/&gt;]
  entries =&gt;  [[Element: &lt;boardgame/&gt;]]
  entries[2] =&gt; undefined
  </code></pre>
  
  <p>Since the bggXml looks exactly how I'd expect but document does not, I assume the problem is in the parsing?</p>