一位客戶希望我向他的網站添加天氣預報。官方天氣報告以 XML 檔案形式提供,我需要幫助才能訪問檔案中的某些元素。
我可以下載包含站點所需資料的兩個 XML 檔案,并將它們決議為 ColdFusion XML 變數。
我可以從頂層提取我需要的資料,但讓我感到胃灼熱的是較低的層級。XML 檔案包含該州每個位置的天氣觀測和預報。我們不需要那個 - 我們只想訪問有關我客戶位置的資料。
這是我正在談論的 XML 資料的示例
<area aac="NSW_PT123" description="Richmond" type="location" parent-aac="NSW_PW005">
<forecast-period index="0" start-time-local="2021-10-16T17:00:00 11:00" end-time-local="2021-10-17T00:00:00 11:00" start-time-utc="2021-10-16T06:00:00Z" end-time-utc="2021-10-16T13:00:00Z">
<element type="forecast_icon_code">2</element>
<text type="precis">Clear.</text>
<text type="probability_of_precipitation">5%</text>
</forecast-period>
<forecast-period index="1" start-time-local="2021-10-17T00:00:00 11:00" end-time-local="2021-10-18T00:00:00 11:00" start-time-utc="2021-10-16T13:00:00Z" end-time-utc="2021-10-17T13:00:00Z">
<element type="forecast_icon_code">1</element>
<element type="air_temperature_minimum" units="Celsius">7</element>
<element type="air_temperature_maximum" units="Celsius">24</element>
<text type="precis">Sunny.</text>
<text type="probability_of_precipitation">0%</text>
</forecast-period>
<forecast-period index="2" start-time-local="2021-10-18T00:00:00 11:00" end-time-local="2021-10-19T00:00:00 11:00" start-time-utc="2021-10-17T13:00:00Z" end-time-utc="2021-10-18T13:00:00Z">
<element type="forecast_icon_code">1</element>
<element type="air_temperature_minimum" units="Celsius">7</element>
<element type="air_temperature_maximum" units="Celsius">28</element>
<text type="precis">Sunny.</text>
<text type="probability_of_precipitation">5%</text>
</forecast-period>
在此代碼段中,index="0"表示“今天”并index="1"表示“明天”。客戶想要 7 天的前景。
所以目前在所有的 XML 資料中,我需要確定它是關于哪一天,圖示代碼(用于漂亮的圖片)和 Precis(通常是一個單詞或短語),probability_of_precipitation(會下雨嗎?)和時間. 剩下的我可以扔掉。
在另一個具有類似結構的檔案中,有一個針對該頁面另一個版本的長格式預測。
我已經能夠訪問第一行(aac和description)中的屬性,但我需要幫助的是如何訪問該行下的預測周期元素。還有forecast-period所有其他地區也一樣,這是我們不希望訪問的元素。
我使用 ColdFusion XmlSearch/XPATH 運算式到達 XML 檔案的這一部分
XmlSearch(IDN10064XML, "//area[@aac='NSW_PT123']")
所以這是我的問題。在上面的示例代碼中,(我無法控制 - 這是政府發布的)我如何創建一個變數來給我結果
"Clear."從上述精確元素?"5%"從降水元素的概率?"2"從forecast_icon_code 元素?
uj5u.com熱心網友回復:
<cffunction name="WeatherForecast" returntype="struct">
<cfargument name="weatherData" type="xml" required="yes">
<cfargument name="areaCode" type="string" required="yes">
<cfset var areas = XmlSearch(weatherData, "//area[@aac='#areaCode#']")>
<cfif ArrayLen(areas) eq 0>
<cfthrow message="WeatherForecast: area code #areaCode# not found.">
</cfif>
<cfset var textOnly = function (node) {
return node.XmlType == 'ATTRIBUTE' ? node.XmlValue : node.XmlText;
}>
<cfreturn {
icons: XmlSearch(areas[1], "./forecast-period/element[@type='forecast_icon_code']").map(textOnly),
precis: XmlSearch(areas[1], "./forecast-period/text[@type='precis']").map(textOnly),
precipitation: XmlSearch(areas[1], "./forecast-period/text[@type='probability_of_precipitation']").map(textOnly)
}>
</cffunction>
用法:
<cfset IDN10064XML = XmlParse(weatherXml, true)>
<cfset forecast = WeatherForecast(IDN10064XML, "NSW_PT123")>
回傳這個結構:
{
"PRECIPITATION": ["5%", "0%", "5%"],
"PRECIS": ["Clear.", "Sunny.", "Sunny."],
"ICONS": ["2", "1", "1"]
}
每個部分將包含與 XML中的<forecast-period>元素一樣多的值<area>,按照它們在 XML 中出現的順序。換句話說,forecast.precipitation[1]將指的是“今天”,除非 XML 有可能出現亂序(我對此表示懷疑)。
可以用同樣的方式提取時間屬性值之類的東西:
XmlSearch(areas[1], "./forecast-period/@start-time-local").map(textOnly)
uj5u.com熱心網友回復:
我更喜歡將簡單的 XML 轉換為 JSON,然后使用常規的結構/陣列處理。
您可以使用 1 行代碼在 Adob??e ColdFusion 中將 XML 轉換為 JSON。
<cfset myJSONObject = createObject("java","org.json.XML").toJSONObject(XMLText)>
<cfdump var="#myJSONObject#">
有關更多示例,請在此處查看示例 CFML:https : //gist.github.com/JamoCA/00bb362672f772fab56d26f3e01ad3fa
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/331665.html
