6

3 つのハチの巣の温度をストリーミングする温度センサーがあり、XML ストリームを解析してセンサーの最後の値を提供できるようにしたいと考えています。

私がしたい:

  • センサー 1: 75 度 (更新: 午後 9 時 4 分)
  • センサー 2: 75 度 (更新: 午後 9 時 4 分)

Google Scripts で次のスクリプトを実行していますが、エラーが発生し続けます。

オブジェクト <?xml version="1.0" encoding="UTF-8"?> に関数 getContentText が見つかりません

簡単なスクリプトは次のとおりです。

function XMLing() {

  var response = UrlFetchApp.fetch("https://api.cosm.com/v2/feeds/79697.xml?key=[private key here]");

  var doc = Xml.parse(response.getContentText(), true);
  var records = doc.getElements("current_value");
  var details = records[0].getText();

  return details;

}

XML は次のとおりです。

<eeml xmlns="http://www.eeml.org/xsd/0.5.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="0.5.1" xsi:schemaLocation="http://www.eeml.org/xsd/0.5.1 http://www.eeml.org/xsd/0.5.1/0.5.1.xsd">
  <environment updated="2012-10-21T00:44:32.162393Z" created="2012-10-10T21:19:43.373591Z" id="79697" creator="https://cosm.com/users/greennomad">
    <private>false</private>
    <data id="sensor1tem">
      <current_value at="2012-10-21T00:44:32.019058Z">67.00</current_value>
      <max_value>618.0</max_value>
      <min_value>611.0</min_value>
    </data>
    <data id="sensor2tem">
      <current_value at="2012-10-21T00:44:32.019058Z">60.57</current_value>
      <max_value>61.5</max_value>
      <min_value>60.41</min_value>
    </data>
...
4

2 に答える 2

1

エラー メッセージは明らかです。応答オブジェクトは実際のテキスト (XML 応答) であり、getContentText() メソッドを持つオブジェクトではありません。したがって、これは機能するはずです:

var doc = Xml.parse(response, true);
于 2012-11-12T08:37:32.607 に答える
0

利用可能な方法を確認できます。

if (!response.getContentText) {
    var props = [];
    for (var p in response) {
        props.push(p);
    }

    var name = typeof response;
    if (response.contructor) name = response.contructor.name;

    return name + " { " + props.join(", ") + " }";
}

私の推測では、UrlFetchApp.fetch()エラーコードが返されるか、XMLドキュメントに対して特別な応答があります。


UrlFetchApp.fetch()エラーメッセージから、XMLドキュメントを直接返しているように見えます。電話する必要はないかもしれませんXml.parse()

function XMLing() {

  var response = UrlFetchApp.fetch("https://api.cosm.com/v2/feeds/79697.xml?key=[private key here]");

  var doc = null;
  if (response.getContentText) {
    doc = Xml.parse(response.getContentText(), true);
  }
  else if (response.getElements) {
    doc = response;
  }
  else {
    var name = typeof response;
    if (response.constructor) name = response.constructor.name;
    throw new Exception("Incompatible type: " + name);
  }

  var records = doc.getElements("current_value");
  var details = records[0].getText();

  return details;

}
于 2012-10-22T07:23:24.067 に答える