user2284253
user2284253

Reputation: 1

How do I parse all values from a given XML element using a Google App Script?

I am trying to parse temperature data into a google spreadsheet from a NOAA source with xml of the form:

..... <parameters applicable-location="point1">
<temperature type="maximum" units="Fahrenheit" time-layout="k-p24h-n7-1">
<name>Daily Maximum Temperature</name>
<value>63</value>
<value>72</value>
<value>76</value>
<value>78</value>
<value>74</value>
<value>62</value>
<value>58</value>
</temperature>
<temperature type="minimum" units="Fahrenheit" time-layout="k-p24h-n6-2">
<name>Daily Minimum Temperature</name>
<value>52</value>
<value>58</value>
<value>56</value>
<value>60</value>
<value>48</value>
<value>45</value>
</temperature>
</parameters>
</data>
</dwml>

I would like each of the numbers inside the tags from both the maximum and minimum headings to be in separate cell in a spreadsheet. My current script is:

function Temperatures() { var text = UrlFetchApp.fetch("http://graphical.weather.gov/xml/SOAP_server/ndfdXMLclient.php?whichClient=NDFDgen&lat=38.99&lon=-77.01&listLatLon=&lat1=&lon1=&lat2=&lon2=&resolutionSub=&listLat1=&listLon1=&listLat2=&listLon2=&resolutionList=&endPoint1Lat=&endPoint1Lon=&endPoint2Lat=&endPoint2Lon=&listEndPoint1Lat=&listEndPoint1Lon=&listEndPoint2Lat=&listEndPoint2Lon=&zipCodeList=&listZipCodeList=&centerPointLat=&centerPointLon=&distanceLat=&distanceLon=&resolutionSquare=&listCenterPointLat=&listCenterPointLon=&listDistanceLat=&listDistanceLon=&listResolutionSquare=&citiesLevel=&listCitiesLevel=&sector=&gmlListLatLon=&featureType=&requestedTime=&startTime=&endTime=&compType=&propertyName=&product=time-series&begin=2004-01-01T00%3A00%3A00&end=2017-04-12T00%3A00%3A00&Unit=e&maxt=maxt&mint=mint&Submit=Submit").getContentText();
return parse(text);
function parse(text) {
var doc = Xml.parse(text, true);
var temps = doc.dwml.data.getElement("parameters").getElement("temperature").getElements("value").getText();
return temps
}
}

However, running this script parses only a single reading—the first tag, 63, into a cell. If I change .getElement(“value”).getText(); to .getElements(“value”).getText(); I receive the following error message: “TypeError: Cannot find function getText in object.”

Thank you for any advice you can provide.

Ron

Upvotes: 0

Views: 3433

Answers (2)

Nate S
Nate S

Reputation: 11

This is an old post but I couldn't find a great answer out there. Here is a pair of scripts that work nicely using the new XmlService, and are iterative to get children of arbitrary depth.

function XMLtoAssocArray(xmlString) {
  var xml = XmlService.parse(xmlString)
  var root = xml.getRootElement()
  return XMLhelper(root)
}


function XMLhelper(Element){
  var children = Element.getChildren()
  var out = {}
  if(children.length>0){
    for (var ii = 0; ii<children.length; ii++){
      var child = children[ii]
      var name = child.getName();
      out[name] = XMLhelper(child)
    }
  } else {
    out = Element.getText()
  }
  return out
}

Upvotes: 1

Brian
Brian

Reputation: 423

First read the elements into an array by calling .getElements("value") and then loop through the result array and getText from each.

var doc = Xml.parse(text, true);
var temps = [];  // temps will be an array
temps = doc.dwml.data.getElement("parameters").getElement("temperature").getElements("value");
for (var i=0; i < temps.length; i++)
{
  var value = temps[i].getText();
  // do something with the value..
  Logger.log(value);
}

Upvotes: 1

Related Questions