From e3327093add815994646826b10fb3503f209efc0 Mon Sep 17 00:00:00 2001 From: Bryan Schumaker Date: Tue, 26 Oct 2010 22:39:04 -0400 Subject: [PATCH] libsaria/xm.py xm.py contains helpful xml parsing functions that we can use for decoding last.fm requests. --- libsaria/xm.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 libsaria/xm.py 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)