5 def __init__(self, fields={}, element=None, keys=None):
7 dict.__init__(self, dict.fromkeys(self.fields))
12 self[key] = fields[key]
15 def get_elements(xml, xpath, element_class=None, fields=None):
17 Search the specifed xml node for elements that match the
18 specified xpath query.
19 Returns a list of objects instanced by the specified element_class.
22 element_class = Element
23 if not fields and hasattr(element_class, 'fields'):
24 fields = element_class.fields
25 elems = xml.xpath(xpath)
29 obj = element_class(elem.attrib, elem)
31 obj = element_class({}, elem)
33 if field in elem.attrib:
34 obj[field] = elem.attrib[field]
39 def add_elements(xml, name, objs, fields=None):
41 Adds a child node to the specified xml node based on
42 the specified name , element class and object.
44 if not isinstance(objs, list):
52 elem = xml.add_element(name)
54 if field in obj and obj[field]:
55 elem.set(field, unicode(obj[field]))