繁体   English   中英

使用 Google 应用脚本从 XML 检索文本

[英]Retrieving text from XML with Google App Script

从下面的天气 function 和 XML 来看,我预计 cloudpc 会返回 id="NN" percent="100.0" 但事实并非如此。 谁能帮我获取 ID 和百分比值。

这是 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>

这是 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)

}

问题

您尝试解析的 xml 包含“id”和“unit”/“percent”属性。 您将无法使用getText()获取这些值。

解决方案

您应该使用getAttribute().getValue() 请参见下面的示例:

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)
}

这将返回:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM