user2035174
user2035174

Reputation: 133

Retrieving text from XML with Google App Script

From the weather function and XML below, I was expecting that cloudpc would return id="NN" percent="100.0" but it doesn't. Can anyone help me to get the ID and percent values.

This is the XML

<weatherdata
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://api.met.no/weatherapi/locationforecast/1.9/schema" created="2021-03-10T13:26:27Z">
<meta>
    <model name="harmonie" termin="2021-03-10T06:00:00Z" runended="2021-03-10T09:49:16Z" nextrun="2021-03-10T16:00:00Z" from="2021-03-10T14:00:00Z" to="2021-03-10T14:00:00Z"/>
</meta>
<product class="pointData">
    <time datatype="forecast" from="2021-03-10T14:00:00Z" to="2021-03-10T14:00:00Z">
        <location altitude="9" latitude="54.7211" longitude="-8.7237">
            <temperature id="TTT" unit="celsius" value="9.2"/>
            <windDirection id="dd" deg="188.4" name="S"/>
            <windSpeed id="ff" mps="12.0" beaufort="6" name="Liten kuling"/>
            <globalRadiation value="41.4" unit="W/m^2"/>
            <humidity value="95.1" unit="percent"/>
            <pressure id="pr" unit="hPa" value="980.9"/>
            <cloudiness id="NN" percent="100.0"/>
            <lowClouds id="LOW" percent="100.0"/>
            <mediumClouds id="MEDIUM" percent="43.1"/>
            <highClouds id="HIGH" percent="0.0"/>
            <dewpointTemperature id="TD" unit="celsius" value="8.4"/>
        </location>
    </time>
    <time datatype="forecast" from="2021-03-10T13:00:00Z" to="2021-03-10T14:00:00Z">
        <location altitude="9" latitude="54.7211" longitude="-8.7237">
            <precipitation unit="mm" value="0.2" minvalue="0.1" maxvalue="0.3" probability="36.9"/>
            <symbol id="Drizzle" number="46"/>
        </location>
    </time>
</product>

This is the function.

function getweather() {
    //
    // Note url below is time sensitive and only works into the future
    //
    url ="http://metwdb-openaccess.ichec.ie/metno-wdb2ts/locationforecast?lat=54.7210798611;long=-8.7237392806;from=2021-03-10T14:00:00Z;to=2021-03-10T14:00:00Z"

    var xml0 = UrlFetchApp.fetch(url, options).getContentText();

    var xml = XmlService.parse(xml0)
    var root = xml.getRootElement()
    var children = root.getChildren()
    var product = children[1].getChildren()
    //Logger.log (product)
    var time1 = product[0].getChildren()
    var location = time1[0].getChildren()
    var cloudiness = location[6]
    var cloudpc = cloudiness.getText()
    Logger.log (cloudpc)

}

Upvotes: 0

Views: 79

Answers (1)

Jason E.
Jason E.

Reputation: 1221

Issue

The xml you are trying to parse contains "id" and "unit"/"percent" attribute. You won't be able to fetch those values using getText().

Solution

You should use getAttribute().getValue(). See example below:

function getweather() {
    //
    // Note url below is time sensitive and only works into the future
    //
    url ="http://metwdb-openaccess.ichec.ie/metno-wdb2ts/locationforecast?lat=54.7210798611;long=-8.7237392806;from=2021-03-10T14:00:00Z;to=2021-03-10T14:00:00Z"

    var xml0 = UrlFetchApp.fetch(url).getContentText();

    var xml = XmlService.parse(xml0)
    var root = xml.getRootElement()
    var children = root.getChildren()
    var product = children[1].getChildren()
    //Logger.log (product)
    var time1 = product[0].getChildren()
    var location = time1[0].getChildren()
    var cloudiness = location[6]
    var cloudid = cloudiness.getAttribute("id").getValue()
    var cloudpct = cloudiness.getAttribute("percent").getValue()
    Logger.log (cloudid + "," + cloudpct)
}

This will return:

enter image description here

Upvotes: 1

Related Questions