def index():
import urllib2
temp = urllib2.urlopen('
http://free.worldweatheronline.com/feed/weather.ashx?q=udupi&format=xml&num_of_days=2&key=b019cd7d6a135850110711
')
tempdata = temp.read()

*# parsing XML string*
parseddata = *TAG(tempdata.replace("<!", "["))  # needs to replace <!
because it gives an error in XML parser
*temp = None*

# looking for tags in XML tree
# <data><current_condition><temp_C>000</temp_C></current_condition></data>
*data = parseddata.elements('data')
if data:
    current_condition = data[0].elements('current_condition')
    if current_condition:
        temp_c = current_condition[0].elements('temp_c')
        if temp_c:
            temp = temp_c[0].flatten()

return dict(temp = temp)

the following code in view:

{{extend 'layout.html'}}

{{=temp}}

----

I tested in my terminal:

>>> def index():
...     import urllib2
...     temp = urllib2.urlopen('
http://free.worldweatheronline.com/feed/weather.ashx?q=udupi&format=xml&num_of_days=2&key=b019cd7d6a135850110711
')
...     tempdata = temp.read()
...
...     # parsing XML string
...     parseddata = TAG(tempdata.replace("<!", "["))  # needs to replace
<! because it gives an error in XML parser
...     temp = None
...
...     # looking for tags in XML tree
...     #
<data><current_condition><temp_C>000</temp_C></current_condition></data>
...     data = parseddata.elements('data')
...     if data:
...         current_condition = data[0].elements('current_condition')
...         if current_condition:
...             temp_c = current_condition[0].elements('temp_c')
...             if temp_c:
...                 temp = temp_c[0].flatten()
...
...     return dict(temp = temp)
>>> index()
24: {'temp': '32'}
On Sat, Mar 10, 2012 at 4:38 AM, Praveen Bhat <praveenbha...@gmail.com>wrote:

> Hello Bruno,
>
> I am sorry to say that I am totally new to programming, so could you
> please eloborate where I can use the above line of code.\
>
> Or you can point to some example .
>
> Regards
> Praveen
>
> On Saturday, March 10, 2012 12:49:11 PM UTC+5:30, rochacbruno wrote:
>>
>> >>> print
>> TAG(tempdata.replace("<!","[")).elements('data')[0].elements('current_condition')[0].elements('temp_c')[0].flatten()
>> 32
>> >>>
>>
>> On Sat, Mar 10, 2012 at 4:04 AM, Praveen Bhat <praveenbha...@gmail.com>wrote:
>>
>>> Hello,
>>>
>>> I am getting the following error:
>>>
>>> '<type 'exceptions.AttributeError'> 'str' object has no attribute
>>> 'elements'
>>>
>>> I have the following code in controller:
>>>
>>> def index():
>>> import urllib2
>>> temp = urllib2.urlopen('
>>> http://free.worldweatheronline.com/feed/weather.ashx?q=udupi&format=xml&num_of_days=2&key=b019cd7d6a135850110711
>>> ')
>>> tempdata = temp.read()
>>> parseddata = XML(tempdata).xml()
>>> return dict(parseddata = parseddata)
>>>
>>> the following code in view:
>>>
>>> {{extend 'layout.html'}}
>>>
>>> {{ for parseddata in parseddata: }}
>>>
>>> <l>{{=XML(parseddata.element("temp_C"))}}</li>
>>> {{pass}}
>>>
>>> Regards
>>> Praveen
>>>
>>>
>>>
>>> On Saturday, March 10, 2012 12:28:18 PM UTC+5:30, rochacbruno wrote:
>>>>
>>>> I guess it will work.
>>>>
>>>> {{ for parseddata in parseddata: }}
>>>>
>>>> <l>{{=XML(parseddata.elements("temp_C"))}}</li>
>>>>
>>>> {{pass}}
>>>>
>>>> On Sat, Mar 10, 2012 at 3:51 AM, Praveen Bhat 
>>>> <praveenbha...@gmail.com>wrote:
>>>>
>>>>> Hello,
>>>>>
>>>>> I apologize as I could not reply...
>>>>>
>>>>> Yes I was able to get the parsed output but I want to display only
>>>>> the  data between < t e m p _ C >  < / t e m p _ C > in the outputted XML
>>>>> and not the whole XMl document....
>>>>>
>>>>>
>>>>> Regards
>>>>> Praveen
>>>>>
>>>>>
>>>>> On Tuesday, March 6, 2012 4:50:21 PM UTC+5:30, yamandu wrote:
>>>>>>
>>>>>> As I can see, you got the XML and outputed it to the view.
>>>>>> I dont see what you want more. Tell us.
>>>>>>
>>>>>> 2012/3/6 Praveen Bhat <praveenbha...@gmail.com>:
>>>>>> > Hello,
>>>>>> >
>>>>>> > I have come to the point where the whole XML document is returned,
>>>>>> as u can
>>>>>> > see in the attached file.
>>>>>> >
>>>>>> > I have used the following code:
>>>>>> >
>>>>>> > Controller:
>>>>>> >
>>>>>> > def index():
>>>>>> > import urllib2
>>>>>> > temp =
>>>>>> > urllib2.urlopen('
>>>>>> http://free.worldweatheronline.com/feed/weather.ashx?q=udupi&format=xml&num_of_days=2&key=b019cd7d6a135850110711
>>>>>> ')
>>>>>> > tempdata = temp.read()
>>>>>> > parseddata = XML(tempdata).xml()
>>>>>> > return dict(parseddata = parseddata)
>>>>>> >
>>>>>> > View:
>>>>>> >
>>>>>> >
>>>>>> > {{ for parseddata in parseddata: }}
>>>>>> >
>>>>>> > <l>{{=XML(parseddata)}}</li>
>>>>>> >
>>>>>> > {{pass}}
>>>>>> >
>>>>>> > I am missing something here. Could you please help.
>>>>>> >
>>>>>> > Regards
>>>>>> >
>>>>>> >
>>>>>> > On Monday, March 5, 2012 10:14:46 PM UTC+5:30, yamandu wrote:
>>>>>> >>
>>>>>> >> You can do something like in
>>>>>> http://web2py.com/books/default/chapter/29/5
>>>>>> >> :
>>>>>> >> Parsing
>>>>>> >>
>>>>>> >> The TAG object is also an XML/HTML parser. It can read text and
>>>>>> >> convert into a tree structure of helpers. This allows manipulation
>>>>>> >> using the API above:
>>>>>> >> >>> html = '<h1>Title</h1><p>this is a <span>test</span></p>'
>>>>>> >> >>> parsed_html = TAG(html)
>>>>>> >> >>> parsed_html.element('span')[0]='TEST'
>>>>>> >> >>> print parsed_html
>>>>>> >> <h1>Title</h1><p>this is a <span>TEST</span></p>
>>>>>> >>
>>>>>> >> 2012/3/5 Praveen Bhat <praveenbha...@gmail.com>:
>>>>>> >> > Hello,
>>>>>> >> >
>>>>>> >> > I am using http://worldweatheronline.com API to display weather
>>>>>> info.
>>>>>> >> > The
>>>>>> >> > API returns the data in XML format.
>>>>>> >> >
>>>>>> >> > How can i render the same in a HTML page in Web2py.
>>>>>> >> >
>>>>>> >> > Link for accessing the API Data:
>>>>>> >> >
>>>>>> >> > Weather API
>>>>>> >> >
>>>>>> >> > I have done the same in PHP as following:
>>>>>> >> >
>>>>>> >> > <?
>>>>>> >> > $xml =
>>>>>> >> >
>>>>>> >> > simplexml_load_file('
>>>>>> http://free.worldweatheronline.com/feed/weather.ashx?q=udupi&format=xml&num_of_days=5&key=b019cd7d6a135850110711'
>>>>>> );
>>>>>> >> > $current = $xml->xpath("/data/current_condition");
>>>>>> >> > ?>
>>>>>> >> >
>>>>>> >> > <div class="weather">
>>>>>> >> > <p><h5>Weather :</h5>
>>>>>> >> > <?= $current[0]->temp_C ?>&deg; C,
>>>>>> >> > <?= $current[0]->weatherDesc ?></p>
>>>>>> >> > </div>
>>>>>> >> >
>>>>>> >> > How can I do the same in web2py?
>>>>>> >> >
>>>>>> >> >
>>>>>> >> > Regards,
>>>>>> >> > Praveen
>>>>>> >> >
>>>>>> >>
>>>>>> >> --
>>>>>> >> Carlos J. Costa
>>>>>> >> Cientista da Computação
>>>>>> >> Esp. Gestão em Telecom
>>>>>> >>
>>>>>> >> EL MELECH NEEMAN!
>>>>>> >> אָמֵן
>>>>>>
>>>>>> --
>>>>>> Carlos J. Costa
>>>>>> Cientista da Computação
>>>>>> Esp. Gestão em Telecom
>>>>>>
>>>>>> EL MELECH NEEMAN!
>>>>>> אָמֵן
>>>>>>
>>>>>
>>>>
>>>>
>>>> --
>>>>
>>>> Bruno Rocha
>>>> [http://rochacbruno.com.br]
>>>>
>>>>
>>
>>
>> --
>>
>> Bruno Rocha
>> [http://rochacbruno.com.br]
>>
>>


-- 

Bruno Rocha
[http://rochacbruno.com.br]

Reply via email to