Parsing XML feeds with AppleScript

I am having some major trouble getting my XML parsing script to work. I have found how to easily parse an XML file saved locally, and I understand how to send the HTTP GET request with curl. I can do these things separately. But how do I get the script to parse the XML feed without saving the XML locally first?

I’m fairly new to AppleScrip so any help would be greatly appreciated.

set theURL to "https://api.mozenda.com/rest?WebServiceKey=4D0BCD88-8DF3-4756-9C11-9042980F74A6&Service=Mozenda10&Operation=Agent.GetList"
set XML to "theURL"
tell application "System Events"
	do shell script "curl " & quoted form of theURL
	tell XML element "MozXML" of contents of XML
		set typeText to (value of XML element "Created")
		set nameText to (value of XML element "name")
		set AgentIDText to (value of XML element "AgentID")
	end tell
end tell

If I remember well, you can’t, you need to save your curl reply to a file.

But it looks more like a job for the scripting additions that come with Smile (http://www.satimage.fr/software/en/index.html)

set XML to XMLOpen theURL bypassing namespace --saves a lot of trouble if namespaces are irrelevant
set XML to XMLRoot XML
set M to XMLFind XML name " MozXML"
 set typeText to XMLGetText(XMLFind M name "Created")
...

or something like that :slight_smile:

Hello.

LateNightSoftware also has a scripting addition for parsing XML, if you download one of the older versions, you’ll get a good set of script examples. (To use with the latest.)

Too bad the site is for 90% compatible with SOAP, which would convert the returned data in records if it was SOAP compatible using AppleScript built-in web services support. Now we can do the call with curl and using AppleScript’s standard addition to convert the XML data into records. There are 3rd party scripting additions that can do the task faster but the code below doesn’t require any other software.


getMozendaFeed("https://api.mozenda.com/rest?WebServiceKey=4D0BCD88-8DF3-4756-9C11-9042980F74A6&Service=Mozenda10&Operation=Agent.GetList")

on getMozendaFeed(URLFeed)
	set RSSItems to {}
	set RSSFeed to do shell script "curl -s " & quoted form of URLFeed
	tell application "System Events"
		set XMLData to make new XML data with data RSSFeed
		repeat with RSSItem in (XML elements of XML element "AgentList" of XML element "AgentGetListResponse" of XMLData)
			set end of RSSItems to {created:value of XML element "Created" of RSSItem, modified:value of XML element "Modified" of RSSItem, agentid:value of XML element "AgentID" of RSSItem, name:value of XML element "Name" of RSSItem, description:value of XML element "Description" of RSSItem}
		end repeat
	end tell
	return RSSItems
end getMozendaFeed