From e0724fb56f4a49fc5da4d6b5ea75dd1029ee9a44 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Sun, 1 Mar 2015 08:22:35 +0100 Subject: [PATCH] fix(json2xml): xml.tostring works now ... ... but it still generates invalid output due to scopes. Should be an easy fix --- etc/json2xml.py | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/etc/json2xml.py b/etc/json2xml.py index b7d935903d4..944443e2cef 100755 --- a/etc/json2xml.py +++ b/etc/json2xml.py @@ -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): @@ -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] @@ -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 @@ -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():