2014-08-27 73 views

回答

0

你真的應該張貼你到目前爲止的代碼,否則它看起來像你知道,沒有編碼,只是要求別人爲你做的工作。 你有兩種選擇。一種是使用幾個XML解析選項之一。但對於像openweathermap網站那樣的小XML文件的一些應用程序,只需對代碼分隔符進行硬編碼即可獲取所需內容。這適用於你所問的內容:

set xmlString to do shell script "curl 'http://api.openweathermap.org/data/2.5/weather?q=NewYork&mode=xml'" 

set otid to AppleScript's text item delimiters 

set AppleScript's text item delimiters to "<temperature value=\"" 
set xmlString to text item 2 of xmlString 
set theTemp to word 1 of xmlString 

set AppleScript's text item delimiters to "<speed value=\"" 
set xmlString to text item 2 of xmlString 
set AppleScript's text item delimiters to "\"" 
set theSpeed to text item 1 of xmlString 

set AppleScript's text item delimiters to otid 

return "Temperature is " & theTemp & " & Speed is " & theSpeed 
+0

你不必在AppleScript中使用shell腳本處理XML,因爲它可以在本地執行,而且你不必將它解析爲文本,因爲AppleScript是面向對象的,你肯定不想用文本項分隔符進行解析,因爲AppleScript沒有內置文本處理的原因是AppleScriptable文本編輯器可以處理文本。 – 2016-01-24 17:47:30