Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e0724fb

Browse files
committed
fix(json2xml): xml.tostring works now ...
... but it still generates invalid output due to scopes. Should be an easy fix
1 parent 143aa6f commit e0724fb

1 file changed

Lines changed: 5 additions & 13 deletions

File tree

etc/json2xml.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@
4545
import json
4646
import optparse
4747
import sys
48-
import os
4948

5049
import xml.etree.cElementTree as ET
50+
from xml.dom import minidom
5151

5252

5353
def strip_tag(tag):
@@ -126,14 +126,6 @@ def internal_to_elem(pfsh, factory=ET.Element):
126126
sublist = []
127127
tags = list(pfsh.keys())
128128

129-
# workaround 'cannot convert boolean error'
130-
def sani_value(v):
131-
if v == u'true':
132-
return '1'
133-
elif v == u'false':
134-
return '0'
135-
return v
136-
137129
# Allow deeply nested structures
138130
for tag in tags:
139131
value = pfsh[tag]
@@ -156,13 +148,13 @@ def sani_value(v):
156148
else:
157149
sublist.append(internal_to_elem({k: v}, factory=factory))
158150
else:
159-
text = sani_value(value)
151+
text = value
160152
e = factory(tag, attribs)
161153

162154
for sub in sublist:
163155
e.append(sub)
164-
e.text = text
165-
e.tail = tail
156+
e.text = unicode(text)
157+
e.tail = unicode(tail)
166158
return e
167159

168160

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

213205
elem = internal_to_elem(json_data, factory)
214-
return ET.tostring(elem)
206+
return minidom.parseString(ET.tostring(elem)).toprettyxml(indent='\t')
215207

216208

217209
def main():

0 commit comments

Comments
 (0)