libsaria/xm.py
xm.py contains helpful xml parsing functions that we can use for decoding last.fm requests.
This commit is contained in:
parent
3680035ba1
commit
e3327093ad
|
@ -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)
|
Loading…
Reference in New Issue