Google脚本阅读xml

时间:2015-11-29 00:09:58

标签: xml

我的xml文件是这样的:

<rowset name="skills" key="typeID" columns="typeID,skillpoints,level,published">
<row typeID="2495" skillpoints="32000" level="3" published="1"/>
<row typeID="2505" skillpoints="63568" level="3" published="1"/>
<row typeID="3184" skillpoints="25562" level="2" published="1"/>
<row typeID="3300" skillpoints="45255" level="4" published="1"/>
<row typeID="3301" skillpoints="8000" level="3" published="1"/>
<row typeID="3310" skillpoints="2829" level="2" published="1"/>
<row typeID="3311" skillpoints="2829" level="2" published="1"/>
<row typeID="3312" skillpoints="2829" level="2" published="1"/>
<row typeID="3315" skillpoints="1000" level="1" published="1"/>
<row typeID="3316" skillpoints="2829" level="2" published="1"/>
<row typeID="3317" skillpoints="1250" level="1" published="1"/>
<row typeID="3318" skillpoints="2829" level="2" published="1"/>
<row typeID="3319" skillpoints="250" level="1" published="1"/>
<row typeID="3327" skillpoints="8000" level="3" published="1"/>
<row typeID="3328" skillpoints="16000" level="3" published="1"/>
<row typeID="3330" skillpoints="500" level="1" published="1"/>
<row typeID="3340" skillpoints="1000" level="1" published="1"/>

当我给出typeID值时,我正在尝试创建读取级别值的函数。我一直试图以多种方式做到这一点,但我失败了。我得到的最好的是找到正确的typeID,但随后它显示了一条消息,“你不能调用”getName“null类的方法”

function skillLvl(id) {
  id=3389;
  var url = "{not important}";
  var document = readXml(url);
  var end = document.getRootElement().getChild("result").getChild("rowset").getChild("row");
  var v1 = end.getAttribute("typeID").toString();
  var v2 = end.getAttribute("level").toString();
  var lvl = 0;
  if (v1 = "4"){
    lvl = v2;
  }

  return lvl;

}

之前还有这个:

<rowset name="jumpClones" key="jumpCloneID" columns="jumpCloneID,typeID,locationID,cloneName">
<row jumpCloneID="23230289" typeID="164" locationID="60003466" cloneName=""/>
<row jumpCloneID="23205549" typeID="164" locationID="60009439" cloneName=""/>
</rowset>

更新:

当调用skillLvl(2495)时它返回“”,这是完整的代码:

function skillLvl(id) {
  //id=3389;
  var idd="skills";
  var url = "(...)";
  var document = readXml(url);
 var rowsets = document.getRootElement().getChild("result").getChildren("rowset");
  var level=null;
for (var i=0;level==null && i<rowsets.length;i++)
{
    var rowset=rowsets[i];
    level=getLevelByTypeFromRowset(rowset, id);
}
// In this line, if level!=null, it is found.
}
function getLevelByTypeFromRowset(rowset, id)
{
    var rows = rowset.getChildren("row");
    var level=null;
    for (var i=0;level==null && i<rows.length;i++)
    {
        var row=rows[i];
        if (row.getAttribute("typeID")==id)
        {
            level=row.getAttribute("level");
        }
    }
    return level;
}

这个可能有问题:

function readXml(url) {
  var parameters = {method : "get", payload : ""};
  var xmlFeed = UrlFetchApp.fetch(url, parameters).getContentText();
  return XmlService.parse(xmlFeed);
}

1 个答案:

答案 0 :(得分:0)

您可以使用getChildren方法获取所有row个节点,然后迭代它们以获得所需的ID:

  var rows = document.getRootElement().getChild("result").getChild("rowset").getChildren("row");
  var level=null;
  for (var i=0;level==null && i<rows.length;i++)
  {
      var row=rows[i];
      var typeIdAttr=row.getAttribute("typeID");
      if (typeIdAttr && typeIdAttr.getValue()==id)
      {
          level=row.getAttribute("level");
      }
  }

<强>更新

如果有多个rowset,您最好在两个功能之间拆分搜索逻辑:

// Main function:
var rowsets = document.getRootElement().getChild("result").getChildren("rowset");
for (var i=0;level==null && i<rowsets.length;i++)
{
    var rowset=rowsets[i];
    level=getLevelByTypeFromRowset(rowset, id);
}
// In this line, if level!=null, it is found.

function getLevelByTypeFromRowset(rowset, id)
{
    var rows = rowset.getChildren("row");
    var level=null;
    for (var i=0;level==null && i<rows.length;i++)
    {
        var row=rows[i];
        var typeIdAttr=row.getAttribute("typeID");
        if (typeIdAttr && typeIdAttr.getValue()==id)
        {
            level=row.getAttribute("level").getValue();
        }
    }
    return level;
}

(如果您可以使用 xpath表达式来简化此逻辑会更好,但我还没有找到如何使用Google Script。)