Skip to content

Commit

Permalink
fix(json2xml): xml.tostring works now ...
Browse files Browse the repository at this point in the history
... but it still generates invalid output due to scopes.
Should be an easy fix
  • Loading branch information
Byron committed Mar 1, 2015
1 parent 143aa6f commit e0724fb
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions etc/json2xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@
import json
import optparse
import sys
import os

import xml.etree.cElementTree as ET
from xml.dom import minidom


def strip_tag(tag):
Expand Down Expand Up @@ -126,14 +126,6 @@ def internal_to_elem(pfsh, factory=ET.Element):
sublist = []
tags = list(pfsh.keys())

# workaround 'cannot convert boolean error'
def sani_value(v):
if v == u'true':
return '1'
elif v == u'false':
return '0'
return v

# Allow deeply nested structures
for tag in tags:
value = pfsh[tag]
Expand All @@ -156,13 +148,13 @@ def sani_value(v):
else:
sublist.append(internal_to_elem({k: v}, factory=factory))
else:
text = sani_value(value)
text = value
e = factory(tag, attribs)

for sub in sublist:
e.append(sub)
e.text = text
e.tail = tail
e.text = unicode(text)
e.tail = unicode(tail)
return e


Expand Down Expand Up @@ -211,7 +203,7 @@ def json2xml(json_data, factory=ET.Element):
json_data = json.loads(json_data)

elem = internal_to_elem(json_data, factory)
return ET.tostring(elem)
return minidom.parseString(ET.tostring(elem)).toprettyxml(indent='\t')


def main():
Expand Down

0 comments on commit e0724fb

Please sign in to comment.