-4

Are you aware of any good Python JSON <-> XML parser that could do the following:

Use the following JSON:

var json = {
    "root_element_name" : {
        "attr1" : "value",
        "attr2" : "value",
        "elements": {
            "element_name1" : "value",
            "element_name2" : "value"
        }
    }
}

and convert to the following XML & vice versa:

<root_element_name attr1="value" attr2="value" >
     <option name="element_name1" value="value"/>
     <option name="element_name2" value="value"/>
</root_element_name>

Any suggestions much appreciated.

Iladarsda
  • 10,250
  • 38
  • 99
  • 165
  • Show us the code you have tried. And please describe how general this problem is. –  Jul 30 '14 at 09:48
  • Did you try with a dict and ElementTree? https://docs.python.org/2/library/xml.etree.elementtree.html – user189 Jul 30 '14 at 09:50
  • 1
    I am looking for recommendations please, I just would like to drop the JSON object and save the XML, without writing any schema if possible. – Iladarsda Jul 30 '14 at 09:51
  • Possible [duplicate](http://stackoverflow.com/questions/191536/converting-xml-to-json-using-python) – sk11 Jul 30 '14 at 09:52
  • Use [json](https://docs.python.org/3/library/json.html) and [xml.etree.elementtree](https://docs.python.org/3/library/xml.etree.elementtree.html). –  Jul 30 '14 at 09:53
  • Recommendations are off-topic on SO. Consider reading through the guidelines on [SR](http://softwarerecs.stackexchange.com/help/on-topic) and see if you can write an appropriate question there. – jonrsharpe Jul 30 '14 at 10:01
  • @jonrsharpe In spite of [highly voted questions](http://stackoverflow.com/questions/191536/converting-xml-to-json-using-python) of similar contents. – joaquin Jul 30 '14 at 10:24
  • @joaquin ...yes. Note that your example is nearly six years old. Per the current close vote reason: *"Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it."* – jonrsharpe Jul 30 '14 at 10:27
  • @jonrsharpe yes I know, you're right. I came here because in the review page this question was tagged as duplicate of the highly voted one. So it was a bit weird situation to tag this one off topic (and see it downvoted) while the other, which is identical, is probably still currently being upvoted and getting upvoted answers (some are not so old). Anyway my comment was probably out of place, as it would fit better for Meta discussion. – joaquin Jul 30 '14 at 13:08

1 Answers1

1

Python a allows you to convert from JSON into a native dict (using json or, in versions < 2.6, simplejson)

You can do this with the help of a library https://github.com/quandyfactory/dict2xml

Or you can do this by loading it into json.loads.For the more explaiantion of this method please have a look here

A sample for converting to xml

  >>> import xmltools

>>> d = {'a':1, 'b':2.2, 'c':'three' }
>>> xx = xmltools.WriteToXMLString(d)
>>> print xx

The result

<?xml version="1.0" encoding="UTF-8"?>
<top>
  <a>1</a>
  <b>2.2</b>
  <c>three</c>
</top>
Community
  • 1
  • 1
Avinash Babu
  • 5,752
  • 2
  • 17
  • 24