8 Replies Latest reply: May 10, 2012 11:50 AM by artpics
abhishekdewan101 Level 1 Level 1 (0 points)

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 script weatherquery as string

          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)
  • 1. Re: XML parsing in Applescript
    billearl Level 2 Level 2 (175 points)

    Sorry, I can't help with your particular problem, but there are a couple free XML osaxen that might help. I use XMLLib osax http://www.satimage.fr/software/en/downloads/downloads_companion_osaxen.html

     

    There is also XML Tools http://www.latenightsw.com/freeware/XMLTools2/index.html

  • 2. Re: XML parsing in Applescript
    Camelot Level 8 Level 8 (45,790 points)

    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 script weatherquery

     

    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.

  • 3. Re: XML parsing in Applescript
    abhishekdewan101 Level 1 Level 1 (0 points)

    thanks a lot the script u gave worked well but m not sure as to how to read the different xml tags information. Any ideas on that.

  • 4. Re: XML parsing in Applescript
    abhishekdewan101 Level 1 Level 1 (0 points)

    I am sorry to bother u this much but i was wondering if u have experience in reading xml data in applescript would it be possible for u to tell me how to do it.. with a small example. I am newbie.. and don't have that much of experience

    Thanks.

  • 5. Re: XML parsing in Applescript
    twtwtw Level 5 Level 5 (4,690 points)

    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 script weatherquery

     

    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.

  • 6. Re: XML parsing in Applescript
    artpics Level 1 Level 1 (30 points)

    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 script weatherquery

     

    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>

  • 7. Re: XML parsing in Applescript
    twtwtw Level 5 Level 5 (4,690 points)

    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

  • 8. Re: XML parsing in Applescript
    artpics Level 1 Level 1 (30 points)

    Thankyou so much it works i was trying to get this working for months and no one had an answer.

     

    now my reef tank temp is inputted into my home automation software.

     

    i will try to get the other value PH etc into my code.

     

    thanks again