How to get and parse an XML file using AppleScript?

There is an XML file on some remote server (http: //foo/bar.xml):

<?xml version="1.0" encoding="UTF-8"?> <foo> bar </foo> 

How can I get the value of "bar" using AppleScript?

+6
source share
3 answers

Here is what I did:

 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 

I originally used the URL Access Scripting application to retrieve the file, but since it was deleted in Lion, I switched to a clean curl that works on both Snow Leopard and Lion.

+4
source

I found this thread , which has an example of parsing an XML file using the XML tools available through System Events. It seems pretty confusing to me though.

There is also this (free) package to add scripts for parsing / writing XML. Do not look at him, but he can be neat.

Personally, I would save my script as a script package, and then I would make a little php / Ruby / perl / python / whatever script to parse the XML (since I'm just more comfortable with this) bundled. Then I will use AppleScript and then pass the XML to the script parser from 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 could be something like this (using Ruby as an example):

 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 and the REXML package should be easily accessible on any Mac, so it should work anywhere ... I suppose)

The point when the script is executed will load the XML file using cURL, pass it to the Ruby script, and at the end of << 22> the value of "bar" will be set in AppleScript.

Of course, if XML is more complex, you will need more scripting or take another look at other parameters.

There are probably even more ways to do this (for example, you could just do some string manipulation instead of completely parsing the XML, but it's a little fragile, of course), but I will stop here :)

+2
source

Understanding that this is an old question, but one of them uses the Bing Maps API (note that I replaced my API key with XXXXXXXXXX ). use do shell script with curl to extract the XML, and then just go through the elements until you get the one you need (you can combine everything tell in tell xml element "X" of xml element "y" of xml element… but it's easier to do).

 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: I suppose I should include the XML that I used for the above:

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

Source: https://habr.com/ru/post/896464/


All Articles