def to_python(self, value): from shopipy.data.models import ShopifyElement if isinstance(value, ShopifyElement): return value # Get XML element node = None if isinstance(value, etree_element): node = value elif isinstance(value, (str, unicode)): node = etree.parse(value) else: raise ValueError("Could not parse XML from value: %s" % repr(value)) # Parse XML element value = self.element_class.parse_xml(node) return value
def parse_xml(cls, node): if isinstance(node, (str, unicode)): node = etree.parse(StringIO(node)).getroot() element = cls() xml_map = {} for field_name, field in element._fields.items(): tag = field.xml_name or field_name xml_map[tag] = field_name, field for child in node: if child.tag not in xml_map: continue field_name, field = xml_map[child.tag] field_value = field.to_python(child) setattr(element, field_name, field_value) return element
def to_python(self, value): # Get XML element node = None if isinstance(value, etree_element): node = value elif isinstance(value, (str, unicode)): node = etree.parse(value) elif isinstance(value, (tuple, list)): return value else: raise ValueError("Could not parse XML from value: %s" % repr(value)) # Parse XML element arr = [] for child in node: if child.tag == self.child_xml_name: arr.append(self.element_class.parse_xml(child)) return arr