2011-09-02 20 views

risposta

4

Ecco che cosa ho fatto:

set file_tgt to (POSIX path of (path to temporary items)) & "file.xml" 
    do shell script "curl -L " & "http://url.com/file.xml" & " -o " & file_tgt 
tell application "System Events" 
    set file_content to contents of XML file file_tgt 
    tell file_content 
     set my_value to value of XML element 1 
    end tell 
end tell 

Originariamente stavo usando l'applicazione "URL di accesso script" per andare a prendere il file, ma dal momento che è stato rimosso in Lion, sono passato a riccio puro, che funziona sotto sia Snow Leopard che Lion.

2

Ho trovato this thread che ha un esempio di analisi di un file XML con gli strumenti XML disponibili tramite Eventi di sistema. Sembra piuttosto complicato per me però.

C'è anche questo (freeware) scripting addition package per l'analisi/scrittura di XML. Non l'ho guardato, ma potrebbe essere pulito.

Personalmente, salverei il mio script come un pacchetto di script, e poi farei un piccolo script php/Ruby/perl/python/qualunque per analizzare l'XML (dato che mi sento più a mio agio con questo) in il pacchetto. Quindi userò AppleScript e poi passerò l'XML allo script parser da cURL.

AppleScript:

set scriptPath to POSIX path of (path to me as alias) & "Contents/Resources/parse_xml.rb" 
set fooValue to do shell script "curl http://foo/test.xml 2> /dev/null | ruby " & quoted form of scriptPath 

parse_xml.rb potrebbe essere somthing simili (usando rubino come esempio):

require "rexml/document" 

# load and parse the xml from stdin 
xml = STDIN.read 
doc = REXML::Document.new(xml) 

# output the text of the root element (<foo>) stripped of leading/trailing whitespace 
puts doc.root.text.strip 

(rubino e il pacchetto REXML dovrebbe essere prontamente disponibili su qualsiasi Mac, quindi dovrebbe funzionare ovunque ... credo)

Il punto è, quando lo script viene eseguito, scaricherà il file XML con cURL, lo passerà allo script Ruby e alla fine, fooValue in AppleScript sarà impostato su "bar".

Ovviamente, se l'XML è più complesso, è necessario più script o dare un'altra occhiata alle altre opzioni.

Probabilmente ci sono ancora più modi per farlo (ad esempio, si può semplicemente manipolare le stringhe invece del parsing XML completo, ma questo è un po 'fragile ovviamente, ma mi fermerò qui :)

0

Realizzare questa è una vecchia domanda, ma ecco un modo per utilizzare l'API di Bing Maps (nota ho sostituito la mia chiave API con XXXXXXXXXX). utilizzare do shell script con curl per recuperare l'XML, quindi basta scendere gli elementi finché non si ottiene quello necessario (è possibile consolidare tutti i tell in tell xml element “X” of xml element “y” of xml element…, ma questo è solo più facile da seguire).

set theXML to make new XML data with properties {name:"Geolocation", text:(do shell script "curl 'http://dev.virtualearth.net/REST/v1/Locations?&q=638%20Brandon%20Town%20Center%20Brandon%20FL%2033511&o=xml&key=XXXXXXXXXX'")} 
tell theXML 
    tell XML element "Response" 
     tell XML element "ResourceSets" 
      tell XML element "ResourceSet" 
       tell XML element "Resources" 
        tell XML element "Location" 
         tell XML element "Point" 
          set theLatitude to the value of XML element "Latitude" 
          set theLongitude to the value of XML element "Longitude" 
         end tell 
        end tell 
       end tell 
      end tell 
     end tell 
    end tell 
end tell 

EDIT: immagino che dovrei includere l'XML che stavo usando per quanto sopra:

<?xml version=\"1.0\" encoding=\"utf-8\"?> 
<Response xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.microsoft.com/search/local/ws/rest/v1\"> 
    <Copyright>Copyright © 2014 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.</Copyright> 
    <BrandLogoUri>http://dev.virtualearth.net/Branding/logo_powered_by.png</BrandLogoUri> 
    <StatusCode>200</StatusCode> 
    <StatusDescription>OK</StatusDescription> 
    <AuthenticationResultCode>ValidCredentials</AuthenticationResultCode> 
    <TraceId>06bb657f1ac9466ba00ef45aa55aef3b|BN20130631|02.00.108.1000|BN2SCH020180822, BN2SCH020181444, BN2SCH020181020, BN2SCH030291220, BN2SCH030261523</TraceId> 
    <ResourceSets> 
    <ResourceSet> 
     <EstimatedTotal>1</EstimatedTotal> 
     <Resources> 
     <Location> 
      <Name>638 Brandon Town Center Dr, Brandon, FL 33511</Name> 
      <Point> 
      <Latitude>27.929752349853516</Latitude> 
      <Longitude>-82.326362609863281</Longitude> 
      </Point> 
      <BoundingBox> 
      <SouthLatitude>27.925889632282839</SouthLatitude> 
      <WestLongitude>-82.332191670122214</WestLongitude> 
      <NorthLatitude>27.933615067424192</NorthLatitude> 
      <EastLongitude>-82.320533549604349</EastLongitude> 
      </BoundingBox> 
      <EntityType>Address</EntityType> 
      <Address> 
      <AddressLine>638 Brandon Town Center Dr</AddressLine> 
      <AdminDistrict>FL</AdminDistrict> 
      <AdminDistrict2>Hillsborough Co.</AdminDistrict2> 
      <CountryRegion>United States</CountryRegion> 
      <FormattedAddress>638 Brandon Town Center Dr, Brandon, FL 33511</FormattedAddress> 
      <Locality>Brandon</Locality> 
      <PostalCode>33511</PostalCode> 
      </Address> 
      <Confidence>High</Confidence> 
      <MatchCode>Good</MatchCode> 
      <GeocodePoint> 
      <Latitude>27.929752349853516</Latitude> 
      <Longitude>-82.326362609863281</Longitude> 
      <CalculationMethod>Parcel</CalculationMethod> 
      <UsageType>Display</UsageType> 
      </GeocodePoint> 
      <GeocodePoint> 
      <Latitude>27.929159164428711</Latitude> 
      <Longitude>-82.32720947265625</Longitude> 
      <CalculationMethod>Interpolation</CalculationMethod> 
      <UsageType>Route</UsageType> 
      </GeocodePoint> 
     </Location> 
     </Resources> 
    </ResourceSet> 
    </ResourceSets> 
</Response> 
Problemi correlati