diff --git a/libsaria/xm.py b/libsaria/xm.py new file mode 100644 index 00000000..98148103 --- /dev/null +++ b/libsaria/xm.py @@ -0,0 +1,50 @@ +# Bryan Schumaker (10/24/2010) + +import xml.dom.minidom as xml + +def attributes(node): + attrs = dict() + for i in range(node.attributes.length): + item = node.attributes.item(i) + attrs[item.name] = item.nodeValue + return attrs + + +def children(doc): + list = [] + for node in doc.childNodes: + if node.nodeType == node.TEXT_NODE: + if node.data.strip() == "": + continue + list += [node] + return list + + +def child(doc): + list = children(doc) + if len(list) > 0: + return list[0] + return None + + +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) + if attr: + return attr + None + + +def parse(xml_file): + return xml.parse(xml_file)