如何使用AppleScript获取和解析XML文件?
有一些远程服务器上的XML文件使用AppleScript如何使用AppleScript获取和解析XML文件?
<?xml version="1.0" encoding="UTF-8"?>
<foo>
bar
</foo>
我怎样才能得到值 “栏中的”:(HTTP://foo/bar.xml)?
这是我做了什么:
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访问脚本”的应用程序,以获取该文件,但由于它在狮子被删除,我切换到纯卷曲,其中工程在雪豹和狮子下。
我找到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解析,但这当然有点脆弱),但我会停在这里:)
认识到这是一个老问题,但这里有一种使用Bing Maps API的方法(注意我用XXXXXXXXXX
替换了我的API密钥)。使用do shell script
和curl
来检索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>