From 9ba602318c79493f83cc5a9efd8d7507a9d532d4 Mon Sep 17 00:00:00 2001 From: Bryan Schumaker Date: Tue, 30 Nov 2010 23:07:46 -0500 Subject: [PATCH] xml write node with attributes The xml functions can now create a node with attributes ( ) --- libsaria/xm.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/libsaria/xm.py b/libsaria/xm.py index b67d9704..742bb679 100644 --- a/libsaria/xm.py +++ b/libsaria/xm.py @@ -24,21 +24,27 @@ def child(doc): return list[0] return None -def add_child(doc, node, name): +def check_type(value): + if value.__class__ == unicode: + return value.encode("utf-8") + return str(value) + +def add_child(doc, node, name, attrs = {}): child = doc.createElement(name) + for attr in attrs: + value = check_type(attrs[attr]) + child.setAttribute(attr, value) node.appendChild(child) return child def add_text(doc, node, value): - if value.__class__ == int or value.__class__ == long: - value = str(value) - elif value.__class__ == unicode: - value = value.encode("utf-8") + value = check_type(value) try: child = doc.createTextNode( value ) node.appendChild(child) return value - except: + except Exception, e: + print e print value, value.__class__ def get_elements(doc, tag):