4

I'm having trouble using xmltodict to convert json to xml. It works fine with a single root and a single object, however when I try to convert multiple objects it returns a ValueError "ValueError: document with multiple roots".

Here's my JSON data:

Here's my script thus far:

import json
import xmltodict

    y = """{{  "markers":[  {  "point":"new GLatLng(40.266044,-74.718479)","awayTeam":"LUGip","markerImage":"images/red.png","fixture":"Wednesday 7pm","information":"Linux users group meets second Wednesday of each month.","previousScore":"","capacity":"","homeTeam":"Lawrence Library"},{  "point":"new GLatLng(40.211600,-74.695702)","awayTeam":"LUGip HW SIG","tv":"","markerImage":"images/white.png","fixture":"Tuesday 7pm","information":"Linux users can meet the first Tuesday of the month to work out harward and configuration issues.","capacity":"","homeTeam":"Hamilton Library"},{  "point":"new GLatLng(40.294535,-74.682012)","awayTeam":"After LUPip Mtg Spot","tv":"","markerImage":"images/newcastle.png","fixture":"Wednesday whenever","information":"Some of us go there after the main LUGip meeting, drink brews, and talk.","capacity":"2 to 4 pints","homeTeam":"Applebees"}]}"""

y2 = json.loads(y)
print(xmltodict.unparse(y2, pretty = True))

Result:

Traceback (most recent call last):

  File "<ipython-input-89-8838ce8b0d7f>", line 1, in <module>
    print(xmltodict.unparse(y2,pretty=True))

  File "/Users/luzazul/anaconda/lib/python3.4/site-packages/xmltodict.py", line 323, in unparse
    raise ValueError('Document must have exactly one root.')

ValueError: Document must have exactly one root.

Any help would be greatly appreciated, thanks!

lagunazul
  • 257
  • 3
  • 5
  • 10
  • Your string `y` isn't valid json or python-- json needs double quotes for dict keys, and python needs triple quotes for a multi-line string. I'm not even sure how you got the result you did with both invalid python and invalid json. – snapshoe Apr 24 '15 at 22:32

2 Answers2

4

Assuming you've cleaned up the input to make it valid (see comment on question)...

It looks like xmltodict is trying to make a markers element for each item in the list, and since markers is at the top level, you're trying to create multiple roots.

I would go about it by adding a top level element around the data like this:

y2 = {'root':y2}
snapshoe
  • 13,454
  • 1
  • 24
  • 28
1

You can use the xmljson library to convert using different XML JSON conventions. It supports multiple roots by returning an array of Elements.

For example:

>>> import xmljson
>>> xmljson.badgerfish.etree({'x': 1, 'y': 2})
[<Element y at 0x3114a08>, <Element x at 0x3114a48>]

-- both x and y elements are returned.

S Anand
  • 11,364
  • 2
  • 28
  • 23