Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing XML with Javascript (in Google Scripts)

I have temperature sensors streaming the temperature of three beehives, and would like to be able to parse the XML stream to provide the last value of the sensor.

I'd like to have:

  • Sensor 1: 75 degrees (updated: 9:04 pm)
  • Sensor 2: 75 degrees (updated: 9:04 pm)

etc.

I'm running following script in Google Scripts, but keep getting an error:

Cannot find function getContentText in object <?xml version="1.0" encoding="UTF-8"?>

Here is the simple script:

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;

}

Here is the 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>
...
like image 637
Matt H Avatar asked Oct 21 '12 01:10

Matt H


People also ask

Is XML parsed by JavaScript?

XML parsing in JavaScript is defined as it is one kind of package or library of the software which provides an interface for the client applications to work with an XML document and it is used in JavaScript to transform the XML document into readable form, nowadays in many browsers, the XML parser is already available ...

Can I use JavaScript in Google App Script?

Google Apps Script is a rapid application development platform that makes it fast and easy to create business applications that integrate with Google Workspace. You write code in modern JavaScript and have access to built-in libraries for favorite Google Workspace applications like Gmail, Calendar, Drive, and more.

How does XML data work in JavaScript?

The XML DOM (Document Object Model) defines the properties and methods for accessing and editing XML. However, before an XML document can be accessed, it must be loaded into an XML DOM object. All modern browsers have a built-in XML parser that can convert text into an XML DOM object.


1 Answers

The error message is fairly obvious: your response object is actual text (the XML response) and not an object which has a getContentText() method. So this should work:

var doc = Xml.parse(response, true);
like image 181
andig Avatar answered Oct 03 '22 07:10

andig