2010-04-07 5 views
2

Я хочу проанализировать Yahoo! Погода API, и я хочу, чтобы сохранить эти атрибуты элементов в переменных для последующего использования:Получение значения атрибута xml с помощью Applescript

<yweather:location city="Zebulon" region="NC" country="US"/> 
<yweather:astronomy sunrise="6:52 am" sunset="7:39 pm"/> 
<yweather:forecast day="Wed" date="7 Apr 2010" low="61" high="96" text="Partly Cloudy" code="29" /> 

Как я могу сделать это с помощью AppleScript?

Так,

set locationCity to [city] 
set locationRegion to [reagion] 
set locationCountry to [country] 

set astronomySunrise to [sunriseTime] 
set astronomySunset to [sunsetTime] 

Я хочу прогнозами быть в массиве своего рода организовать их день/дата

ответ

2

Системные события имеет небольшой набор родных команд, которые вы можете получить вокруг (непроверенный):

(* 
Assuming the following dummy XML: 

<?xml version="1.0" encoding="UTF-8"?> 
<RootTag> 
    <yweather:location city="Zebulon" region="NC" country="US"/> 
    <yweather:astronomy sunrise="6:52 am" sunset="7:39 pm"/> 
    <yweather:forecast day="Wed" date="7 Apr 2010" low="61" high="96" text="Partly Cloudy" code="29" /> 
</RootTag> 

*) 

    set theXMLFile to ((choose file) as string) 

    tell application "System Events" 
     set theXMLData to contents of XML file theXMLFile 
     tell theXMLData 
      set theXMLRoot to XML element 1 
     set locationTag to XML element 1 of theXMLRoot 
     set cityAttribute to XML attribute 1 of locationTag 
     return cityAttribute --> "Zebulon" 
     end tell 
    end tell 

из того, что я собираю XML крючков AppleScript является очень привередливым о его XML и не предлагает много способов поиска неисправностей, кроме «некоторых данных не из ожидаемый тип ".

Я отправляюсь в другое место для разбора XML, и я рекомендую то же самое здесь. Если требуется чистое решение Applescript, я знаю только XML Tools addition from Late Night Software (бесплатно) и Satimage has a more complex set bundled with their suite (оплачивается), но у меня нет опыта с ним.

Смежные вопросы