Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Really simple way to deal with XML in Python?

Tags:

Musing over a recently asked question, I started to wonder if there is a really simple way to deal with XML documents in Python. A pythonic way, if you will.

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 http://www.google.com/ig/api?weather=94043

<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="Mountain View, CA"/>       <postal_code data="94043"/>       <latitude_e6 data=""/>       <longitude_e6 data=""/>       <forecast_date data="2010-06-23"/>       <current_date_time data="2010-06-24 00:02:54 +0000"/>       <unit_system data="US"/>     </forecast_information>     <current_conditions>       <condition data="Sunny"/>       <temp_f data="68"/>       <temp_c data="20"/>       <humidity data="Humidity: 61%"/>       <icon data="/ig/images/weather/sunny.gif"/>       <wind_condition data="Wind: NW at 19 mph"/>     </current_conditions>     ...     <forecast_conditions>       <day_of_week data="Sat"/>       <low data="59"/>       <high data="75"/>       <icon data="/ig/images/weather/partly_cloudy.gif"/>       <condition data="Partly Cloudy"/>     </forecast_conditions>   </weather> </xml_api_reply> 

After loading/parsing such document, i would like to be able to access the information as simple as say

>>> xml['xml_api_reply']['weather']['forecast_information']['city'].data 'Mountain View, CA' 

or

>>> xml.xml_api_reply.weather.current_conditions.temp_f['data'] '68' 

From what I saw so far, seems that ElementTree 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?)

PS. Note I have tried BeautifulSoup already and while I like its approach, it has real issues with empty <element/>s - see below in comments for examples.

like image 347
Nas Banov Avatar asked Jun 24 '10 00:06

Nas Banov


People also ask

How does Python handle XML?

To read an XML file using ElementTree, firstly, we import the ElementTree class found inside xml library, under the name ET (common convension). Then passed the filename of the xml file to the ElementTree. parse() method, to enable parsing of our xml file. Then got the root (parent tag) of our xml file using getroot().

How do I read a simple XML file in Python?

To read an XML file, firstly, we import the ElementTree class found inside the XML library. Then, we will pass the filename of the XML file to the ElementTree. parse() method, to start parsing. Then, we will get the parent tag of the XML file using getroot() .

Does XML work with Python?

The Python standard library provides a minimal but useful set of interfaces to work with XML. The two most basic and broadly used APIs to XML data are the SAX and DOM interfaces. Simple API for XML (SAX) − Here, you register callbacks for events of interest and then let the parser proceed through the document.


1 Answers

lxml has been mentioned. You might also check out lxml.objectify for some really simple manipulation.

>>> from lxml import objectify >>> tree = objectify.fromstring(your_xml) >>> tree.weather.attrib["module_id"] '0' >>> tree.weather.forecast_information.city.attrib["data"] 'Mountain View, CA' >>> tree.weather.forecast_information.postal_code.attrib["data"] '94043' 
like image 189
Ryan Ginstrom Avatar answered Sep 27 '22 21:09

Ryan Ginstrom