Note that there are some explanatory texts on larger screens.

plurals
  1. POReally simple way to deal with XML in Python?
    primarykey
    data
    text
    <p>Musing over a <a href="https://stackoverflow.com/questions/3063319/">recently asked question</a>, I started to wonder if there is a <em>really simple</em> way to deal with XML documents in Python. A pythonic way, if you will.</p> <p>Perhaps I can explain best if i give example: let's say the following - which i think is a good example of how XML is (mis)used in web services - is the response i get from http request to <a href="http://www.google.com/ig/api?weather=94043" rel="nofollow noreferrer">http://www.google.com/ig/api?weather=94043</a></p> <pre><code>&lt;xml_api_reply version="1"&gt; &lt;weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" &gt; &lt;forecast_information&gt; &lt;city data="Mountain View, CA"/&gt; &lt;postal_code data="94043"/&gt; &lt;latitude_e6 data=""/&gt; &lt;longitude_e6 data=""/&gt; &lt;forecast_date data="2010-06-23"/&gt; &lt;current_date_time data="2010-06-24 00:02:54 +0000"/&gt; &lt;unit_system data="US"/&gt; &lt;/forecast_information&gt; &lt;current_conditions&gt; &lt;condition data="Sunny"/&gt; &lt;temp_f data="68"/&gt; &lt;temp_c data="20"/&gt; &lt;humidity data="Humidity: 61%"/&gt; &lt;icon data="/ig/images/weather/sunny.gif"/&gt; &lt;wind_condition data="Wind: NW at 19 mph"/&gt; &lt;/current_conditions&gt; ... &lt;forecast_conditions&gt; &lt;day_of_week data="Sat"/&gt; &lt;low data="59"/&gt; &lt;high data="75"/&gt; &lt;icon data="/ig/images/weather/partly_cloudy.gif"/&gt; &lt;condition data="Partly Cloudy"/&gt; &lt;/forecast_conditions&gt; &lt;/weather&gt; &lt;/xml_api_reply&gt; </code></pre> <p>After loading/parsing such document, i would like to be able to access the information as simple as say </p> <pre><code>&gt;&gt;&gt; xml['xml_api_reply']['weather']['forecast_information']['city'].data 'Mountain View, CA' </code></pre> <p>or</p> <pre><code>&gt;&gt;&gt; xml.xml_api_reply.weather.current_conditions.temp_f['data'] '68' </code></pre> <p>From what I saw so far, seems that <code>ElementTree</code> is the closest to what I dream of. But it's not there, there is still some fumbling to do when consuming XML. OTOH, what I am thinking is not that complicated - probably just thin veneer on top of a parser - and yet it can decrease annoyance of dealing with XML. Is there such a magic? (And if not - why?)</p> <p>PS. Note I have tried <code>BeautifulSoup</code> already and while I like its approach, it has real issues with empty <code>&lt;element/&gt;</code>s - see below in comments for examples.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload