- namespaces = self.namespaces
- return self.node.xpath(xpath, namespaces=namespaces)
-
- def add_element(name, *args, **kwds):
- element = etree.SubElement(name, args, kwds)
- return XmlNode(element, self.namespaces)
-
- def remove_elements(name):
+ namespaces = self.namespaces
+ elems = self.element.xpath(xpath, namespaces=namespaces)
+ return [XmlElement(elem, namespaces) for elem in elems]
+
+ def add_element(self, tagname, **kwds):
+ element = etree.SubElement(self.element, tagname, **kwds)
+ return XmlElement(element, self.namespaces)
+
+ def append(self, elem):
+ if isinstance(elem, XmlElement):
+ self.element.append(elem.element)
+ else:
+ self.element.append(elem)
+
+ def getparent(self):
+ return XmlElement(self.element.getparent(), self.namespaces)
+
+ def get_instance(self, instance_class=None, fields=None):
+ """
+ Returns an instance (dict) of this xml element. The instance
+ holds a reference to this xml element.
+ """
+ if fields is None:
+ fields = []
+ if not instance_class:
+ instance_class = Element
+ if not fields and hasattr(instance_class, 'fields'):
+ fields = instance_class.fields
+
+ if not fields:
+ instance = instance_class(self.attrib, self)
+ else:
+ instance = instance_class({}, self)
+ for field in fields:
+ if field in self.attrib:
+ instance[field] = self.attrib[field]
+ return instance
+
+ def add_instance(self, name, instance, fields=None):
+ """
+ Adds the specifed instance(s) as a child element of this xml
+ element.
+ """
+ if fields is None:
+ fields = []
+ if not fields and hasattr(instance, 'keys'):
+ fields = instance.keys()
+ elem = self.add_element(name)
+ for field in fields:
+ if field in instance and instance[field]:
+ elem.set(field, unicode(instance[field]))
+ return elem
+
+ def remove_elements(self, name):