0

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

2 Answers 2

1

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);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Brian, I appreciate your insight quick response. Your suggestion makes a lot of sense. However, after I added your addition and the line "return value" to my script, I continue to get output of just "63" in a single cell rather than the intended "63" in cell A1, "72" in cell A2..."45" in cell A13. I feel that I may be missing a step. Any thoughts? Thanks.
You have two issues. when you write "return value" it will exit the "for loop" after only one pass. You need to write the value to the spreadsheet on each pass...there are many ways you can do that, however try this ..."return value;" with this SpreadsheetApp.getActiveSpreadsheet().getSheets().[0].appendRow([value]);
............. with this SpreadsheetApp.getActiveSpreadsheet().getSheets().[0].appendRow([value]); Note: if you are are not in an embeded spreadsheet and are in a standalone App Script, use SpreadsheetApp.open(FILE_ID) instead. The other issue you have is that you will not be able to parse all of these at once, only the values in each temperature element.....
You could also load the values into an array (results[i] = value;) and then return back the array to the calling function (return results)
1

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.