繁体   English   中英

嵌套时,getAttributes返回[function getValue]

[英]getAttributes returning [function getValue] when in a nested for

我的第二个示例是返回[function getValue]的示例,我正在尝试对其进行修复,但我看不出问题出在哪里。

我一直在Google脚本中弄乱xmlparse,我要解析的xml将所有相关数据保留在元素的属性中。

这是该xml格式的示例: https : //api.eveonline.com/account/characters.xml.aspx?keyID=1409941&vCode=xagxMH966J2EQinVpoFOBB5H1UidCwsjoTwtBKhhvMVZWqq6Jio4mkiBwv026Olc

这是一些有效的代码(通过日志[ctrl] + [enter]显示):

function dialogDisplay() {
  var xmlstring = Xml.parse('<rowset name="characters" key="characterID" columns="name,characterID,corporationName,corporationID"><row name="Jonah Younbrog" characterID="90131303" corporationName="House of Praetor" corporationID="523373135"/><row name="Mador Younbrog" characterID="90149709" corporationName="House of Praetor" corporationID="523373135"/><row name="Marc Younbrog" characterID="747451028" corporationName="House of Praetor" corporationID="523373135"/></rowset>');
  var attributes = xmlstring.getElement().getAttributes();
  for (var i in attributes) {
    Logger.log(attributes[i].getValue());
  }
}

这是不起作用的代码,它也成功记录了元素名称并使用嵌套的fors来遍历xml:

function fetchToLogger() {
  var assetURL = "https://api.eveonline.com/account/characters.xml.aspx?keyID=1409941&vCode=xagxMH966J2EQinVpoFOBB5H1UidCwsjoTwtBKhhvMVZWqq6Jio4mkiBwv026Olc";
  var assetstring = UrlFetchApp.fetch(assetURL).getContentText();
  var xmlstring = Xml.parse(assetstring, false);

  var elements = xmlstring.eveapi.result.getElements();
  for (var a in elements) {

    Logger.log(elements[a].getName().getLocalName());

    var attributes = elements[a].getAttributes();
    for (var x in attributes) {

      Logger.log(attributes[x].getValue);
    }

    var subelements = elements[a].getElements();
    for (var b in subelements) {

      Logger.log(subelements[b].getName().getLocalName());

      var subattributes = subelements[b].getAttributes();
      for (var y in attributes) {

        Logger.log(attributes[y].getValue);
      }

    }

  }

}

您没有在调用实际函数,请更改为:

Logger.log(attributes[x].getValue());

请注意, Logger.log(attributes[x].getValue只是对函数的引用,这就是控制台显示的内容。

.getValue是一个函数。 因此,您应该使用.getValue()例如:

Logger.log(attributes[x].getValue());

暂无
暂无

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

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