2010-04-07 3 views
2

Je souhaite analyser le compte Yahoo! API météo et je veux sauver ces attributs d'éléments variables pour une utilisation ultérieure:Obtention de la valeur d'un attribut xml avec 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" /> 

Comment puis-je faire avec AppleScript?

Ainsi,

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

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

Je veux que les forcasts soient dans un tableau de quelque sorte de les organiser par jour/date

Répondre

2

System Events a un petit ensemble de commandes natives qui peuvent vous se déplacer (non testé):

(* 
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 

de ce que je comprends les crochets XML AppleScript sont très pointilleux à ce sujet est XML et ne propose pas beaucoup de la manière de dépannage autre que « certaines données non du type attendu ".

Je vais ailleurs pour mon analyse XML et je recommande la même chose ici. Si une solution Applescript pure est nécessaire, je ne connais que le XML Tools addition from Late Night Software (gratuit) et Satimage has a more complex set bundled with their suite (payé), mais je n'ai aucune expérience avec l'un ou l'autre.