Skip to content Skip to sidebar Skip to footer

Convert Json To XML Properly

I have following dictionary structure [{ 'Body' : [{ 'Universal Lift Support' : ['1\'-9\' Extended Length', '10\'-14.5\' Extended Le

Solution 1:

Consider using built-in Python libraries (json, xml.etree.ElementTree, and to pretty print, xml.dom.minidom) that traverses down json object and builds XML tree. One thing to note: XML nodes cannot contain spaces in names, so it should <UniversalLiftSupport>.

import json
import xml.etree.ElementTree as ET
import xml.dom.minidom

with open('BodyUniversal.json') as f:  
    jsondata = json.load(f)

# INITIALIZING XML DOC AND PARENT TAGS
root = ET.Element('root')
body = ET.SubElement(root, 'Body')
uls = ET.SubElement(body, 'UniversalLiftSupport')
uls.text = ''

# ITERATE THROUGH LIST, APPENDING TO XML
for i in jsondata[0]['Body'][0]['Universal Lift Support']:
    uls.text = uls.text + '\n\t\t\t' + i

# OUTPUT AND PRETTY PRINT TREE
tree_out = ET.tostring(root, encoding="UTF-8")
newXML = xml.dom.minidom.parseString(tree_out.decode('UTF-8'))
pretty_xml = newXML.toprettyxml()    

print(pretty_xml)
# <?xml version="1.0" ?>
# <root>
#         <Body>
#                 <UniversalLiftSupport>
#                         1&quot;-9&quot; Extended Length
#                         10&quot;-14.5&quot; Extended Length
#                         15&quot;-19&quot; Extended Length
#                         20&quot; + Extended Length</UniversalLiftSupport>
#         </Body>
# </root>

# OUTPUT XML CONTENT TO FILE
with open('Output.xml','w') as f:
    f.write(pretty_xml)

Solution 2:

Probably you'll have to code a function to generate the xml for the list the way you want: https://github.com/quandyfactory/dicttoxml/blob/master/dicttoxml.py

(see dicttoxml function comment)

Seems the "<item>whatever</item>" pattern is hardcoded and if you use cdata=True you'll get the whole '<![CDATA[whatever]]>' stuff.


Solution 3:

If the software is called dicttoxml, then I think it would be reasonable to expect it to generate XML. The output that you say you want is not XML, so it's not surprising that this tool doesn't generate it. You seem to be complaining that the product does what it says on the tin.


Post a Comment for "Convert Json To XML Properly"