From 71dd8fd1380a70ac18cd3e28dcd0c392237001bd Mon Sep 17 00:00:00 2001 From: Antony Lesuisse Date: Wed, 5 Oct 2011 20:15:29 +0200 Subject: [PATCH] [FIX] forgot to add xml2json.py bzr revid: al@openerp.com-20111005181529-dbfsmptdi671l94z --- addons/web/common/xml2json.py | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 addons/web/common/xml2json.py diff --git a/addons/web/common/xml2json.py b/addons/web/common/xml2json.py new file mode 100644 index 00000000000..16022624fe7 --- /dev/null +++ b/addons/web/common/xml2json.py @@ -0,0 +1,38 @@ +# xml2json-direct +# Simple and straightforward XML-to-JSON converter in Python +# New BSD Licensed +# +# URL: http://code.google.com/p/xml2json-direct/ + +class Xml2Json(object): + @staticmethod + def convert_to_json(s): + return simplejson.dumps( + Xml2Json.convert_to_structure(s), sort_keys=True, indent=4) + + @staticmethod + def convert_to_structure(s): + root = ElementTree.fromstring(s) + return Xml2Json.convert_element(root) + + @staticmethod + def convert_element(el, skip_whitespaces=True): + res = {} + if el.tag[0] == "{": + ns, name = el.tag.rsplit("}", 1) + res["tag"] = name + res["namespace"] = ns[1:] + else: + res["tag"] = el.tag + res["attrs"] = {} + for k, v in el.items(): + res["attrs"][k] = v + kids = [] + if el.text and (not skip_whitespaces or el.text.strip() != ''): + kids.append(el.text) + for kid in el: + kids.append(Xml2Json.convert_element(kid)) + if kid.tail and (not skip_whitespaces or kid.tail.strip() != ''): + kids.append(kid.tail) + res["children"] = kids + return res