1

So I have an XML string that I want to convert to JSON in Python 3.2. I was going to do that by converting the string to a python object via lxml and then convert the object to JSON via JSONEncoder.

Unfortunately, lxml does not seem to be built for Win32, does anyone have any suggestions for an alternate XML - Python library or XML - JSON library?

varunsrin
  • 860
  • 2
  • 15
  • 24
  • http://users.skynet.be/sbi/libxml-python/ – Tim Yates May 28 '11 at 21:14
  • im using python 3.2 - the latest one there is for python 2.7 – varunsrin May 28 '11 at 21:18
  • Is it lxml the Python package or libxml the C library that you can't find builds for? I see a bunch for the former from http://pypi.python.org/pypi/lxml/2.3 – Tim Yates May 28 '11 at 21:24
  • ...and for the latter from http://www.zlatkovic.com/libxml.en.html. I'm not sure how the versions match, but there are plenty of other Google hits. – Tim Yates May 28 '11 at 21:26
  • the installers on http://pypi.python.org/pypi/lxml/2.3 do not detect that I have Python installed because they couldn't find it in the registry. Anyone know what reg keys they are looking for? – varunsrin May 28 '11 at 21:46
  • How did you install Python? The standard Win32 MSI package sets the registry keys. See http://effbot.org/zone/python-register.htm for a fix. Please, try Google before asking, and accept more of your questions. People here will be much more willing to answer them. – Tim Yates May 28 '11 at 22:02
  • See also http://stackoverflow.com/questions/191536/converting-xml-to-json-using-python/191617#191617 . – johnsyweb May 28 '11 at 22:37
  • I installed Python with the MSI package, which has added the correct registry keys. I did Google this, and the the problem is that the registry keys I saw in the search results were already in my system. That is why I asked again here. – varunsrin May 28 '11 at 22:49

2 Answers2

1

I have a super simple script that does this:

XML to Python data structure « Python recipes « ActiveState Code http://code.activestate.com/recipes/534109-xml-to-python-data-structure/

It is mainly for people to reference XML data as an object in Python. For your purpose, you can introspect the node's content in DataNode._attrs and .data.

Wai Yip Tung
  • 18,106
  • 10
  • 43
  • 47
1

xml.etree.ElementTree and its faster sibling xml.etree.cElementTree are included with all Python versions from 2.5 onwards. The Python 3.2 docs are here.

lxml.etree is an implementation of the ElementTree interface with some enhancements and (well-documented) minor differences. However the structure of an Element instance is the same. Is there any particular reason why you must have lxml?

John Machin
  • 81,303
  • 11
  • 141
  • 189