Parsing XML on a Google Apps script

后端 未结 1 1904
迷失自我
迷失自我 2021-01-23 12:08

Here\'s my XML:

 

    
        
            

        
相关标签:
1条回答
  • 2021-01-23 12:49

    How about:

    function getRecords(xml) {
      var xmlDoc = XmlService.parse(xml);
    
      var response = xmlDoc.getRootElement();
      var records = response.getChild('records');
      var recordList = records.getChildren('record');
    
      var output = [];
      for (var i = 0, record; record = recordList[i]; i++) {
        var columnList = record.getChildren('column');
        var name = '';
        var email = '';
        for (var j = 0, column; column = columnList[j]; j++) {
          var attrName = column.getAttribute('name').getValue();
          var text = column.getChild('value').getText();
          if (attrName === 'Name') {
            name = text;
          } else if (attrName === 'Email') {
            email = text;
          }
        }
        output.push([name, email]);
      }
      return output;
    }
    

    This function takes a string argument, being your XML, and returns a two-dimensional array, with each row of the array being name and email address pairs.

    0 讨论(0)
提交回复
热议问题