如何使用AppleScript获取和解析XML文件?

如何使用AppleScript获取和解析XML文件?,applescript,Applescript,某个远程服务器上有一个XML文件(http://foo/bar.xml): 酒吧 如何使用AppleScript获取值“bar”?我发现它有一个示例,可以通过系统事件使用XML工具解析XML文件。但对我来说似乎很复杂 还有这个(免费软件)用于解析/编写XML。我没看过,但它可能很整洁 就我个人而言,我会将我的脚本保存为脚本包,然后我会制作一些php/Ruby/perl/python/任何脚本来解析包中的XML(因为我对它比较熟悉)。然后我将使用AppleScript,然后将XML从cURL传

某个远程服务器上有一个XML文件(http://foo/bar.xml):


酒吧
如何使用AppleScript获取值“bar”?

我发现它有一个示例,可以通过系统事件使用XML工具解析XML文件。但对我来说似乎很复杂

还有这个(免费软件)用于解析/编写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可能是这样的(以Ruby为例):

需要“rexml/document”
#从stdin加载并解析xml
xml=STDIN.read
doc=REXML::Document.new(xml)
#输出除去前导/尾随空格的根元素()的文本
放置doc.root.text.strip
(Ruby和REXML包应该可以在任何Mac上随时使用,所以它应该可以在任何地方工作……我相信)

关键是,当脚本运行时,它将下载带有cURL的XML文件,并将其传递给Ruby脚本,最后,AppleScript中的
fooValue
将设置为“bar”

当然,如果XML更复杂,则需要编写更多脚本,或者再看看其他选项

可能有更多的方法可以做到这一点(例如,您可以只进行一些字符串操作,而不是完全进行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访问脚本”应用程序获取文件,但由于它在Lion中已被删除,我转而使用pure curl,它可以在Snow Leopard和Lion下工作。

意识到这是一个老问题,但这里有一种使用Bing Maps API的方法(注意,我用
xxxxxxxxx
替换了我的API密钥)。使用
do shell script
curl
来检索XML,然后沿着元素往下走,直到找到所需的元素(您可以将所有
tell
s合并到
tell XML元素“X”或XML元素“y”的
XML元素中……
,但这更容易理解)

编辑:我想我应该包括我在上面使用的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>

版权所有©2014微软及其供应商。版权所有。未经微软公司明确书面许可,不得访问本API,不得以任何方式使用、复制或传播本API的内容和任何结果。
http://dev.virtualearth.net/Branding/logo_powered_by.png
200
好啊
有效存款
06bb657f1ac9466ba00ef45aa55aef3b | BN20130631 | 02.00.108.1000 | BN2SCH020180822、BN2SCH020181444、BN2SCH020181020、BN2SCH030291220、BN2SCH030261523
1.
638佛罗里达州布兰登市布兰登市中心Dr 33511
27.929752349853516
-82.326362609863281
27.925889632282839
-82.332191670122214
27.933615067424192
-82.320533549604349
地址
638布兰登市中心
佛罗里达州
希尔斯堡公司。
美国
638佛罗里达州布兰登市布兰登市中心Dr 33511
布兰登
33511
高
好
27.929752349853516
-82.326362609863281
包裹
展示
27.929159164428711
-82.32720947265625
插值
路线
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
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 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>