【发布时间】:2018-08-28 13:50:50
【问题描述】:
我目前正在尝试从 BGG xml api 中提取有关棋盘游戏的信息。流程是这样的:
用户输入棋盘游戏名称作为搜索->
脚本轮询 api 以查找匹配该名称的游戏并返回结果->
用户选择了正确的结果->
脚本从选择的结果中获取棋盘游戏 ID,并使用提供所有需要信息的 ID 再次轮询 API。
这是我正在处理的代码:
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();
这是我从搜索中收到的输出:
<boardgames termsofuse="[https://boardgamegeek.com/xmlapi/termsofuse](https://boardgamegeek.com/xmlapi/termsofuse)">
<boardgame objectid="238032">
<name primary="true">Betrayal at Baldur\'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\'s Walk</name>
<yearpublished>2016</yearpublished>
</boardgame>
</boardgames>\
我可以从那里处理其他所有事情。我遇到的问题是棋盘游戏的 ID 包含在一个 xml 标记 <boardgame objectid="xxxxxx"> 中,我不知道如何让脚本访问该信息。当我查看日志中的输出时,我可以看到 ID,但我不知道如何将 ID 提取到变量中。我基本上没有 xml 经验,我敢肯定这就是为什么我无法弄清楚一些看起来相当简单的东西。我在谷歌表(基本上是javascript)上使用谷歌应用程序脚本。
This 是我发现的帮助我入门的帖子,但他的用例似乎不需要 ID。很遗憾,我知道。
任何想法或建议将不胜感激!
【问题讨论】: