summaryrefslogtreecommitdiff
path: root/addons/web/common/xml2json.py
diff options
context:
space:
mode:
authorAntony Lesuisse <al@openerp.com>2011-10-05 20:15:29 +0200
committerAntony Lesuisse <al@openerp.com>2011-10-05 20:15:29 +0200
commit71dd8fd1380a70ac18cd3e28dcd0c392237001bd (patch)
treeedb204102244e62d73fee52ccad6341f3ffce5ca /addons/web/common/xml2json.py
parent545765173e58865238d7d90f27cf5d16866b2b93 (diff)
[FIX] forgot to add xml2json.py
bzr revid: al@openerp.com-20111005181529-dbfsmptdi671l94z
Diffstat (limited to 'addons/web/common/xml2json.py')
-rw-r--r--addons/web/common/xml2json.py38
1 files changed, 38 insertions, 0 deletions
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