You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用ColdFusion提取指定区域XML天气数据中forecast-period的特定元素

Extracting Specific Forecast Data from XML in ColdFusion

Hey Mike, nice work getting that target <area> node pinpointed with your initial XPath query! Let’s build on that to pull out the exact forecast details you need for Richmond. Here’s a step-by-step approach with code examples:

1. First, Capture the Target Area Node

Since XmlSearch() returns an array of matching nodes, we’ll grab the first (and only, in your case) result for the Richmond area:

<cfset targetArea = XmlSearch(IDN10064XML, "//area[@aac='NSW_PT123']")[1]>

2. Fetch All Forecast Periods

Next, we’ll get every <forecast-period> child node under the target area. Using the relative XPath ./forecast-period ensures we only look within the Richmond node, not the entire XML:

<cfset forecastPeriods = XmlSearch(targetArea, "./forecast-period")>

3. Loop Through Periods & Extract Data

Now we can iterate over each forecast period and pull out the specific elements and attributes you need. We’ll use XmlSearch() again for nested elements, and XmlGetAttribute() for the time/date attributes:

<cfloop array="#forecastPeriods#" index="period">
    <!--- Grab time/date attributes --->
    <cfset periodIndex = XmlGetAttribute(period, "index")>
    <cfset startTimeLocal = XmlGetAttribute(period, "start-time-local")>
    <cfset endTimeLocal = XmlGetAttribute(period, "end-time-local")>

    <!--- Extract forecast icon code --->
    <cfset iconCodeNode = XmlSearch(period, "./element[@type='forecast_icon_code']")[1]>
    <cfset forecastIconCode = iconCodeNode.XmlText>

    <!--- Extract short weather description (precis) --->
    <cfset precisNode = XmlSearch(period, "./text[@type='precis']")[1]>
    <cfset precisText = precisNode.XmlText>

    <!--- Extract precipitation probability --->
    <cfset precipProbNode = XmlSearch(period, "./text[@type='probability_of_precipitation']")[1]>
    <cfset precipProbability = precipProbNode.XmlText>

    <!--- Use the data however you need (example output below) --->
    <cfoutput>
        <div class="forecast-day">
            <h4>#periodIndex == 0 ? "Today" : periodIndex == 1 ? "Tomorrow" : "Day #periodIndex#"#</h4>
            <p>Time Range: #DateFormat(startTimeLocal, "dd/mm/yyyy")# #TimeFormat(startTimeLocal, "HH:mm")# to #DateFormat(endTimeLocal, "dd/mm/yyyy")# #TimeFormat(endTimeLocal, "HH:mm")#</p>
            <p>Weather: #precisText# (Icon Code: #forecastIconCode#)</p>
            <p>Chance of Rain: #precipProbability#</p>
        </div>
    </cfoutput>
</cfloop>

Key Notes:

  • Array Indexing: XmlSearch() always returns an array, so we add [1] to grab the first matching node (since each period will have exactly one of each element you’re targeting).
  • Relative XPath: Using ./ in the XPath queries ensures we only search within the current <forecast-period> node, avoiding accidental matches elsewhere in the XML.
  • Long Text Forecasts: For your second XML file with long-form forecasts, the logic is identical—just adjust the XPath to target the relevant <text> element type (e.g., ./text[@type='forecast'] or whatever the attribute is named in that file).

This should give you all the variables you need to populate the 7-day forecast on your client’s site!

内容的提问来源于stack exchange,提问作者MikeKear

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 20:49:06