2011-09-02 30 views

Trả lời

4

đây là những gì tôi đã thực hiện:

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 

Ban đầu tôi đã sử dụng "URL truy cập Scripting" ứng dụng để lấy các tập tin, nhưng vì nó đã được gỡ bỏ trong Lion, tôi chuyển sang curl tinh khiết, mà làm việc dưới cả Snow Leopard và Lion.

2

Tôi đã tìm thấy this thread có ví dụ về phân tích cú pháp tệp XML với các công cụ XML có sẵn thông qua Sự kiện hệ thống. Có vẻ khá phức tạp với tôi mặc dù.

Còn có điều này (phần mềm miễn phí) scripting addition package để phân tích cú pháp/ghi XML. Đã không nhìn vào nó, nhưng nó có thể gọn gàng.

Cá nhân, tôi sẽ lưu tập lệnh của mình dưới dạng tập lệnh và sau đó tôi sẽ tạo một đoạn mã nhỏ/Ruby/perl/python/bất kỳ tập lệnh nào để phân tích cú pháp XML (vì tôi chỉ cảm thấy thoải mái hơn) Bó. Sau đó, tôi sẽ sử dụng AppleScript sau đó chuyển XML sang tập lệnh phân tích cú pháp từ 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 có thể là somthing như thế này (sử dụng Ruby như một ví dụ):

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 

(Ruby và gói REXML nên có sẵn trên bất kỳ máy Mac, vì vậy nó sẽ hoạt động ở bất cứ đâu… Tôi tin)

Điểm là, khi tập lệnh chạy nó sẽ tải xuống tệp XML bằng cURL, chuyển nó sang tập lệnh Ruby và cuối cùng, fooValue trong AppleScript sẽ được đặt thành "thanh".

Tất nhiên, nếu XML phức tạp hơn, bạn sẽ cần thêm tập lệnh hoặc xem xét các tùy chọn khác. Có lẽ thậm chí còn nhiều cách để thực hiện nó (ví dụ, bạn chỉ có thể thực hiện một số thao tác chuỗi thay vì phân tích cú pháp XML đầy đủ, nhưng đó là một chút giòn), nhưng tôi sẽ dừng lại ở đây :)

0

Nhận ra đây là câu hỏi cũ, nhưng dưới đây là một cách sử dụng API Bing Maps (lưu ý tôi đã thay thế khóa API bằng XXXXXXXXXX). sử dụng do shell script với curl để truy xuất XML, sau đó chỉ cần đi xuống các phần tử cho đến khi bạn nhận được phần tử bạn cần (bạn có thể hợp nhất tất cả các tell s thành tell xml element “X” of xml element “y” of xml element…, nhưng điều này chỉ dễ làm theo).

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: Tôi cho rằng tôi nên bao gồm XML Tôi đã sử dụng cho các phần trên:

<?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> 
Các vấn đề liên quan