[Tutor] Is there any XML lib like the default json lib in terms of usability?

Peter Otten __peter__ at web.de
Fri Sep 19 09:45:45 CEST 2014


Juan Christian wrote:

> Using the default json lib is easy and straightforward:
> 
> import json
> info = json.loads('file.json')
> # Use dict and list to navigate through 'info'
> 
> 
> Sadly, I'm working in a script and the only format the server gives is
> XML, I searched for a good alternative and didn't find anything as easy as
> the default json lib.
> 
> Any tips?

I have never used it, but lxml.objectify seems pretty intuitive:

>>> from lxml import objectify
>>> a = objectify.fromstring("<a><b foo='bar'>10</b><c>20</c></a>")
>>> a.b
10
>>> a = objectify.fromstring("<a><b foo='bar'>10</b><c>20</c><c>30</c></a>")
>>> a.b
10
>>> a.b**2
100
>>> a.b.get("foo")
'bar'
>>> a.c
20
>>> a.c[1]
30
>>> len(a.c)
2

See <http://lxml.de/objectify.html>



More information about the Tutor mailing list