简体   繁体   English

在Google Apps脚本中为BGG API解析XML

[英]Parsing XML in Google Apps Script for BGG API

I'm currently trying to pull information on Boardgames from the BGG xml api. 我目前正在尝试从BGG xml API中获取有关Boardgames的信息。 The flow is something like this: 流程是这样的:

User enters boardgame name as a search-> 用户输入棋盘游戏名称作为搜索->

script polls the api for games matching that name and returns the results-> 脚本会轮询api以查找与该名称匹配的游戏,并返回结果->

User picks the correct result-> 用户选择正确的结果->

script takes the boardgame ID from the picked result and polls the api again using the ID which gives all the needed info. 脚本从选择的结果中获取棋盘游戏ID,然后使用提供所有所需信息的ID再次轮询api。

Here is the code I'm working on: 这是我正在处理的代码:

var Name = "Betrayal at the house on the hill"

  // Call BGG API with query to display options for selection
  var response = UrlFetchApp.fetch("https://boardgamegeek.com/xmlapi/search?search="+Name)

  // Parse the XML reply
  var document = XmlService.parse(response);
  var root = document.getRootElement();

Here is the output I receive from the search: 这是我从搜索中收到的输出:

<boardgames termsofuse="[https://boardgamegeek.com/xmlapi/termsofuse](https://boardgamegeek.com/xmlapi/termsofuse)">
        <boardgame objectid="238032">

        <name primary="true">Betrayal at Baldur\&#039;s Gate and House on the Hill Promo Cards</name>           
<yearpublished>2017</yearpublished>
</boardgame>

        <boardgame objectid="10547">

        <name primary="true">Betrayal at House on the Hill</name>           
<yearpublished>2004</yearpublished>
</boardgame>

        <boardgame objectid="198452">

        <name primary="true">Betrayal at House on the Hill: Widow\&#039;s Walk</name>           
<yearpublished>2016</yearpublished>
</boardgame>

</boardgames>\

I can handle everything else from there. 我可以在那里处理其他所有事情。 The issue I'm having, is that the boardgame's ID is wrapped up in an xml tag <boardgame objectid="xxxxxx"> , and I can't figure out how to get the script to access that information. 我遇到的问题是,桌游的ID包装在xml标签<boardgame objectid="xxxxxx"> ,而我不知道如何获取脚本来访问该信息。 I can see the ID when I look at the output in the logs, but I can't figure out how to pull the ID into a variable. 查看日志中的输出时,我可以看到ID,但是我不知道如何将ID提取到变量中。 I have basically no xml experience which I'm sure is why I can't figure out something that seems fairly simple. 我基本上没有xml经验,因此可以肯定,这就是为什么我无法弄清楚看起来很简单的东西的原因。 I'm using google apps script on a google sheet (basically javascript). 我在Google工作表(基本上是JavaScript)上使用了Google Apps脚本。

This is a post I found that helped me get started, but he doesn't seem to need the ID for his use case. 我发现是一篇帮助我入门的帖子,但是他的用例似乎不需要ID。 Sadly, I do. 可悲的是。

Any thoughts or suggestions would be much appreciated! 任何想法或建议将不胜感激!

Zelyn on reddit was able to answer my question. Zedyn在reddit上回答了我的问题。 The necessary code to pull the ID is below: 提取ID所需的代码如下:

var document = XmlService.parse(bggXml);
var root = document.getRootElement();
var games = root.getChildren('boardgame');

for (var i = 0; i < games.length; i++) {
    var id = games[i].getAttribute('objectid').getValue();
    // do something with id
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM