Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

XML parsing in Applescript

Hi i am trying to parse a xml file and my getting the following error please can anyone help me

Can’t make «class xmld» {"<?xml version=\"1.0\"?><xml_api_reply version=\"1\"><weather module_id=\"0\" tab_id=\"0\" mobile_row=\"0\" mobile_zipped=\"1\" row=\"0\" section=\"0\" ><forecast_information><city data=\"Dehli\"/><postal_code data=\"Dehli\"/><latitude_e6 data=\"\"/><longitude_e6 data=\"\"/><forecast_date data=\"2011-10-14\"/><current_date_time data=\"1970-01-01 00:00:00 +0000\"/><unit_system data=\"US\"/></forecast_information><current_conditions><condition data=\"Haze\"/><temp_f data=\"84\"/><temp_c data=\"29\"/><humidity data=\"Humidity: 37%\"/><icon data=\"/ig/images/weather/haze.gif\"/><wind_condition data=\"Wind: NW at 9 mph\"/></current_conditions><forecast_conditions><day_of_week data=\"Fri\"/><low data=\"70\"/><high data=\"93\"/><icon data=\"/ig/images/weather/mostly_sunny.gif\"/><condition data=\"Mostly Sunny\"/></forecast_conditions><forecast_conditions><day_of_week data=\"Sat\"/><low data=\"68\"/><high data=\"93\"/><icon data=\"/ig/images/weather/sunny.gif\"/><condition data=\"Clear\"/></forecast_conditions><forecast_conditions><day_of_week data=\"Sun\"/><low data=\"66\"/><high data=\"91\"/><icon data=\"/ig/images/weather/sunny.gif\"/><condition data=\"Clear\"/></forecast_conditions><forecast_conditions><day_of_week data=\"Mon\"/><low data=\"66\"/><high data=\"91\"/><icon data=\"/ig/images/weather/sunny.gif\"/><condition data=\"Clear\"/></forecast_conditions></weather></xml_api_reply>"} of application "System Events" into type XML data.


and the code my trying to implement is



set weatherquery to " /usr/bin/curl 'http://www.google.com/ig/api?weather=Dehli'"


tell application "System Events"


set xmltext to do shell scriptweatherqueryasstring

set file1 to XML data [xmltext] as XML data


end tell


It would be helpful if someone could help me with it.

MacBook Pro, Mac OS X (10.7.1)

Posted on Oct 14, 2011 9:23 AM

Reply
10 replies

Oct 14, 2011 10:12 AM in response to abhishekdewan101

I, too, had problems coercing the text into XML data - not sure whether that's just insufficient coffee or whether I'm missing something, too.


However, the System Events' XML suite certainly seems focussed on XML files, rather than arbitrary strings, so one workaround (hack?) is to save the data to a file and read that:


set weatherquery to " /usr/bin/curl 'http://www.google.com/ig/api?weather=Dehli' > /tmp/weather.xml"

do shell scriptweatherquery


tell application "System Events"


set file1 to contents of XML file "/tmp/weather.xml"

set d to every XML element of file1


end tell

So here, curl writes the data to a temp file which System Events is then happy to read and process.

Oct 14, 2011 1:14 PM in response to abhishekdewan101

just for an example, to extract the current humidity you would do this:


set weatherquery to " /usr/bin/curl 'http://www.google.com/ig/api?weather=Dehli' > /tmp/weather.xml"

do shell scriptweatherquery


tell application "System Events"

set file1 to XML file "/tmp/weather.xml"

tell file1

tell XML element "xml_api_reply"

tell XML element "weather"

tell XML element "current_conditions"

tell XML element "humidity"

get value of XML attribute "data"

end tell

end tell

end tell

end tell

end tell

end tell


That can be condensed, obviously, I just spelled out all the tells so you could see how it works.

May 10, 2012 11:04 AM in response to twtwtw

i have been trying to use your script and many others but still cannot get this to work here is my XML below. i am on LION OSX


i am trying to get the data 78.4 which is under the "Temp" line


here is your modified code. the IP below has been changed please test by using the XML below


set weatherquery to " /usr/bin/curl 'http://333.175.99.111:101/cgi-bin/status.xml' > /tmp/weather.xml"

do shell scriptweatherquery


tell application "System Events"

set file1 to XML file "/tmp/weather.xml"

tell file1

tell XML element "probes"

tell XML element "probe"

tell XML element "name"

tell XML element "value"

get value of XML attribute "value"

end tell

end tell

end tell

end tell

end tell

end tell



and here is the error i keep getting.

thanks to anyone who can help me


error "System Events got an error: Can’t get XML element \"probes\" of XML file \"Macintosh HD:private:tmp:weather.xml\"." number -1728 from XML element "probes" of XML file "Macintosh HD:private:tmp:weather.xml"



<?xml version="1.0"?>

<status software="4.12L_2A12" hardware="1.0">

<hostname>apex</hostname>

<serial>AC4:04376</serial>

<timezone>-8</timezone>

<date>05/10/2012 10:50:08</date>

<power><failed>04/26/2012 08:55:46</failed>

<restored>04/26/2012 08:56:20</restored></power>

<probes>

<probe>

<name>Temp</name>

<value>78.4</value>

<type>Temp</type>

</probe>

<probe>

<name>pH</name> <value>8.27</value>

<type>pH</type></probe>

<probe>

<name>Amp_3</name> <value>0.0 </value>

<type>Amps</type></probe></probes>

<outlets>

<outlet>

<name>zSndAlm_I6</name>

<outputID>0</outputID>

<state>AOF</state>

<deviceID>base_Alarm</deviceID>

</outlet>

<outlet>

<name>zSndWrn_I7</name>

<outputID>1</outputID>

<state>AOF</state>

<deviceID>base_Warn</deviceID>

</outlet>

<outlet>

<name>zEmailAlm_I5</name>

<outputID>2</outputID>

<state>AOF</state>

<deviceID>base_email</deviceID>

</outlet>

<outlet>

<name>HighPump</name>

<outputID>3</outputID>

<state>AON</state>

<deviceID>3_1</deviceID>

</outlet>

<outlet>

<name>Skimmer</name>

<outputID>4</outputID>

<state>AON</state>

<deviceID>3_2</deviceID>

</outlet>

<outlet>

<name>LowPump</name>

<outputID>5</outputID>

<state>AON</state>

<deviceID>3_3</deviceID>

</outlet>

<outlet>

<name>Heater</name>

<outputID>6</outputID>

<state>AOF</state>

<deviceID>3_4</deviceID>

</outlet>

<outlet>

<name>Vortech</name>

<outputID>7</outputID>

<state>AON</state>

<deviceID>3_5</deviceID>

</outlet>

<outlet>

<name>Fan</name>

<outputID>8</outputID>

<state>AOF</state>

<deviceID>3_6</deviceID>

</outlet>

<outlet>

<name>Reactor</name>

<outputID>9</outputID>

<state>AON</state>

<deviceID>3_7</deviceID>

</outlet>

<outlet>

<name>Radion</name>

<outputID>10</outputID>

<state>AOF</state>

<deviceID>3_8</deviceID>

</outlet>

<outlet>

<name>Feed</name>

<outputID>11</outputID>

<state>OFF</state>

<deviceID>Cntl:A1</deviceID>

</outlet>

<outlet>

<name>Maintain</name>

<outputID>12</outputID>

<state>OFF</state>

<deviceID>Cntl:A2</deviceID>

</outlet>

<outlet>

<name>z----</name>

<outputID>13</outputID>

<state>OFF</state>

<deviceID>Cntl:A3</deviceID>

</outlet>

<outlet>

<name>z----------</name>

<outputID>14</outputID>

<state>OFF</state>

<deviceID>Cntl:A4</deviceID>

</outlet>

</outlets></status>

May 10, 2012 11:34 AM in response to artpics

you've gotten your hierarchy mixed up. The top level element is 'status', and the 'value' element is next to the 'name' element, not inside it. try this:


tell application "System Events"

set file1 to XML file "/tmp/weather.xml"

tell file1

tell XML element "status"

tell XML element "probes"


-- there are multiple probe elements. First is default, but you can specify whichever you want

tell (first XML element whose name is "probe")

set theValue to value of XML element "value"

end tell

end tell

end tell

end tell

end tell

Jun 17, 2015 7:04 PM in response to twtwtw

That helped me so much knowing you can do 1st, 2nd and 3rd etc. Is there a way to return only the names of people who have scored tries in the below XML feed?


<scorer>

<player_id>4685</player_id>

<player_name>Nick Phipps</player_name>

<player_short>N. Phipps</player_short>

<score_type>TRY</score_type>

<period>1</period>

<score_time>12</score_time>

<home_score>5</home_score>

<away_score>0</away_score>

</scorer>

<scorer>

<player_id>4929</player_id>

<player_name>Bernard Foley</player_name>

<player_short>B. Foley</player_short>

<score_type>CONOK</score_type>

<period>1</period>

<score_time>14</score_time>

<home_score>7</home_score>

<away_score>0</away_score>

</scorer>

<scorer>

<player_id>4929</player_id>

<player_name>Bernard Foley</player_name>

<player_short>B. Foley</player_short>

<score_type>PGOK</score_type>

<period>1</period>

<score_time>33</score_time>

<home_score>10</home_score>

<away_score>7</away_score>

</scorer>

<scorer>

<player_id>4929</player_id>

<player_name>Bernard Foley</player_name>

<player_short>B. Foley</player_short>

<score_type>TRY</score_type>

<period>1</period>

<score_time>39</score_time>

<home_score>15</home_score>

<away_score>12</away_score>

</scorer>


Cheers,

Jul 1, 2015 10:34 AM in response to David.Cherrie

David.Cherrie wrote:


Is there a way to return only the names of people who have scored tries in the below XML feed?



Here's one way. Read the xml data into a list of lists and then create a list of matches.


SG



set theData to "<main>

<scorer>

<player_id>4685</player_id>

<player_name>Nick Phipps</player_name>

<player_short>N. Phipps</player_short>

<score_type>TRY</score_type>

<period>1</period>

<score_time>12</score_time>

<home_score>5</home_score>

<away_score>0</away_score>

</scorer>

<scorer>

<player_id>4929</player_id>

<player_name>Bernard Foley</player_name>

<player_short>B. Foley</player_short>

<score_type>CONOK</score_type>

<period>1</period>

<score_time>14</score_time>

<home_score>7</home_score>

<away_score>0</away_score>

</scorer>

<scorer>

<player_id>4929</player_id>

<player_name>Bernard Foley</player_name>

<player_short>B. Foley</player_short>

<score_type>PGOK</score_type>

<period>1</period>

<score_time>33</score_time>

<home_score>10</home_score>

<away_score>7</away_score>

</scorer>

<scorer>

<player_id>4929</player_id>

<player_name>Bernard Foley</player_name>

<player_short>B. Foley</player_short>

<score_type>TRY</score_type>

<period>1</period>

<score_time>39</score_time>

<home_score>15</home_score>

<away_score>12</away_score>

</scorer>

</main>"


set listOfLists to {}


tell application "System Events"

set xmlData to makenewXML datawith properties {name:"xmldata", text:theData}

tell XML element "main" of xmlData-- here Main is the root element

tell XML element "scorer"

set theNames to name of XML elements


--> {"player_id", "player_name", "player_short", "score_type", "period", "score_time", "home_score", "away_score"}

end tell


repeat with i from 1 to (count XML elements)

tell XML elementi

set R to {}

repeat with j from 1 to count theNames

set R to R & (its value of XML elements whose name is (theNames's item j))

end repeat

end tell

set listOfLists to listOfLists & {R}

end repeat


--> {{"4685", "Nick Phipps", "N. Phipps", "TRY", "1", "12", "5", "0"}, {"4929", "Bernard Foley", "B. Foley", "CONOK", "1", "14", "7", "0"}, {"4929", "Bernard Foley", "B. Foley", "PGOK", "1", "33", "10", "7"}, {"4929", "Bernard Foley", "B. Foley", "TRY", "1", "39", "15", "12"}}

end tell

end tell


set namesOfTrys to {}

repeat with i in listOfLists's items

if i's item 4 is "TRY" then copy i's item 2 to end of namesOfTrys

end repeat

return namesOfTrys-->{"Nick Phipps", "Bernard Foley"}

XML parsing in Applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.