使用Google Apps脚本解析XML [英] Parse XML using Google Apps Script

查看:101
本文介绍了使用Google Apps脚本解析XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在解析来自boardgamegeek查询的XML时遇到困难,因此我可能会用数据填充Google工作表.这是bgg xml的示例:

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:

<boardgames termsofuse="http://boardgamegeek.com/xmlapi/termsofuse">
  <boardgame objectid="423">
    <yearpublished>1995</yearpublished>
    <minplayers>3</minplayers>
    <maxplayers>6</maxplayers>
    <playingtime>300</playingtime>
    <name primary="true" sortindex="1">1856</name>
  </boardgame>
</boardgames>

这是我为解析而编写的Google Apps脚本:

And here's the Google Apps Script I've written to parse it:

//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 < 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);

由于条目为NULL,我目前在for循环中遇到错误.如果我注释掉循环并记录bggXml的外观,则它看起来与上面的示例相同.但是,进一步记录变量,我得到以下信息:

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:

document => [Document:  No DOCTYPE declaration, Root is [Element: <boardgames/>]]
root => [Element: <boardgames/>]
entries =>  [[Element: <boardgame/>]]
entries[2] => undefined

由于bggXml看起来完全符合我的预期,但文档却没有,我认为问题出在解析中?

Since the bggXml looks exactly how I'd expect but document does not, I assume the problem is in the parsing?

推荐答案

经过反复的尝试和绊脚石,我找到了所需的解决方案.这将获取单个xml元素的值并将其设置为变量:

After much trial and error and stumbling through the dark, I found the solution I was looking for. This will get the value of an individual xml element and set it to a variable:

var yearpublished = root.getChild('boardgame').getChild('yearpublished').getText();

所以我的最终代码如下所示.希望它对您的工作有所帮助.

So my final code looks like this. I hope it helps you in your endeavors.

//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();

  //set variables to data from bgg
  var yearpublished = root.getChild('boardgame').getChild('yearpublished').getText();
  var minplayers = root.getChild('boardgame').getChild('minplayers').getText();
  var maxplayers = root.getChild('boardgame').getChild('maxplayers').getText();
  var playingtime = root.getChild('boardgame').getChild('playingtime').getText();
  var name = root.getChild('boardgame').getChild('name').getText();

  //populate sheet with variable data
  SpreadsheetApp.getActiveSheet().getRange(i+1,1).setValue(name);
  SpreadsheetApp.getActiveSheet().getRange(i+1,4).setValue(minplayers);
  SpreadsheetApp.getActiveSheet().getRange(i+1,5).setValue(maxplayers);
  SpreadsheetApp.getActiveSheet().getRange(i+1,5).setValue(playingtime);
  SpreadsheetApp.getActiveSheet().getRange(i+1,7).setValue(yearpublished);

如果您碰巧也在查询BGG,则有多个名称元素.我希望将主要属性设置为"true"的那个.遍历这些元素以找到正确的元素将是我的下一个挑战.

In case you happen to also be querying BGG, there are multiple name elements. I want the one with the primary attribute set to "true". Iterating through those elements to find the correct one will be my next challenge.

这篇关于使用Google Apps脚本解析XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆