2011-09-02 158 views

回答

4

這是我做了什麼:

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 

原來我用的是「URL訪問腳本」的應用程序,以獲取該文件,但由於它在獅子被刪除,我切換到純捲曲,其中工程在雪豹和獅子下。

2

我找到this thread,它有一個使用可通過System Events獲得的XML工具解析XML文件的示例。但似乎相當複雜,但我。

還有這個(免費軟件)scripting addition package解析/寫入XML。沒有看過它,但它可能是整潔的。我個人將腳本保存爲一個腳本包,然後編寫一個小小的php/Ruby/perl/python /任何腳本來解析XML(因爲我更喜歡這個)捆綁。然後我會使用AppleScript,然後將XML從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可能是財產以後所示(使用紅寶石作爲示例):

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 

(紅寶石和REXML包應該是任何Mac上容易獲得的,所以它應該在任何地方工作......我相信)

要點是,當腳本運行時,它會用cURL下載XML文件,並將它傳遞給Ruby腳本,最後,AppleScript中的將被設置爲「bar」。

當然,如果XML更復雜,您需要更多的腳本,或者再看看其他選項。

有可能還有更多的方法來做到這一點(例如,你可以做一些字符串操作,而不是全面的XML解析,但這當然有點脆弱),但我會停在這裏:)

0

認識到這是一個老問題,但這裏有一種使用Bing Maps API的方法(注意我用XXXXXXXXXX替換了我的API密鑰)。使用do shell scriptcurl來檢索XML,然後沿着元素走直到找到所需的元素(您可以將所有tell s合併到tell xml element 「X」 of xml element 「y」 of xml element…,但這只是更容易遵循)。

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 

編輯:我想我應該包括我用了上面的XML:

<?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>