From 8b2a0b9433aedb6e26f4fa2e19c6981dc3850fe1 Mon Sep 17 00:00:00 2001 From: Bryan Schumaker Date: Fri, 12 Nov 2010 20:46:19 -0500 Subject: [PATCH] Added xml creation functions These functions are designed to help create an xml document. --- libsaria/xm.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/libsaria/xm.py b/libsaria/xm.py index 98148103..b67d9704 100644 --- a/libsaria/xm.py +++ b/libsaria/xm.py @@ -9,7 +9,6 @@ def attributes(node): attrs[item.name] = item.nodeValue return attrs - def children(doc): list = [] for node in doc.childNodes: @@ -19,25 +18,38 @@ def children(doc): list += [node] return list - def child(doc): list = children(doc) if len(list) > 0: return list[0] return None +def add_child(doc, node, name): + child = doc.createElement(name) + 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") + try: + child = doc.createTextNode( value ) + node.appendChild(child) + return value + except: + print value, value.__class__ def get_elements(doc, tag): return doc.getElementsByTagName(tag) - def find_attribute(doc, tag, attr, value): for node in get_elements(doc, tag): if attributes(node)[attr] == value: return node return None - def find_preferred_attribute(doc, tag, attr, values): for value in values: attr = find_attribute(doc, tag, attr, value) @@ -45,6 +57,8 @@ def find_preferred_attribute(doc, tag, attr, values): return attr None +def new(): + return xml.Document() def parse(xml_file): return xml.parse(xml_file)