谷歌脚本解析XML [英] google script parse xml

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

问题描述

XML:

 <issues total_count="210" offset="0" limit="25" type="array">
    <issue>
    <id>22079</id>
    <project id="21" name="name1"/>
    <description>Some text</description>
    <start_date>2017-11-22</start_date>
    <custom_fields type="array">
    <custom_field id="36" name="cf_36">
    <value>cf_36_value</value>
    </custom_field>
    <custom_field id="28" name="cf_28">
    <value>cf_28_value</value>
    </custom_field>
    <value type="array"/>
    </custom_field>
    </custom_fields>
    </issue>

我正在尝试获取循环中所有字段的值,然后将数据插入表中.

Hi, I'm trying to get the value of all the fields in the loop, then insert data into sheet.

function APIRequest (url,sheet) {

  var xml = UrlFetchApp.fetch(url).getContentText();

  var DOC = Xml.parse(xml);
  var issues = DOC.issues.issue

  for(var c=0; c<issues.length;c++){
   var issue = issues[c];
    var id = issue.getElement('id').getText();

  sheet.appendRow([id]);
  }
 }

我的问题是我无法从XML中获取每个问题"的值"cf_36_value"

My problem is that I cann't get the values "cf_36_value" from XML for each "issue"

推荐答案

这个答案怎么样?您可以使用Google Apps脚本的XML Service Service解析XML数据.从您的问题和脚本中,我理解了您想要做的事情.

How about this answer? You can parse XML data using XML Service Service for Google Apps Script. From your question and script, I understood what you want to do as follows.

  • 您要使用Google Apps脚本创建脚本.
  • 问题"中有问题"的几个要素.
  • 问题"中有一个"custom_fields"元素.
  • "custom_fields"中有"custom_field"的几个元素.
  • 您想要具有属性id="36"的元素下的值.
    • 那是cf_36_value.
    • You want to create the script using Google Apps Script.
    • There are several elements of "issue" in "issues".
    • There are one element of "custom_fields" in "issue".
    • There are several elements of "custom_field" in "custom_fields".
    • You want the value under the element with the attribute of id="36".
      • That's cf_36_value.

      以上几点反映的修改后的脚本如下.

      The modified script reflected above points is as follows.

      var data = XmlService.parse(xml);
      var issue = data.getRootElement().getChildren("issue");
      var result = [];
      for (i in issue) {
        var custom_field = issue[i].getChild("custom_fields").getChildren("custom_field");
        for (j in custom_field) {
          if (custom_field[j].getAttribute("id").getValue() == 36) { // If you want values of other ID, please set this.
            result.push([custom_field[j].getChild("value").getValue()]);
          }
        }
      }
      sheet.getRange(sheet.getLastRow() + 1, 1, result.length, result[0].length).setValues(result);
      

      参考文献:

      • XML服务服务
      • References :

        • XML Service Service
        • 如果我误解了您的问题,请告诉我.您可以向我们展示整个XML数据吗?我想修改我的答案.由于您问题的XML数据不完整,因此我对此感到担心.

          If I misunderstand your question, please tell me. And can you show us whole XML data? I would like to modify my answer. Since the XML data of your question is incompleted, I worry about it.

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

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