def render_element_node(self, parent: Element, value: Any, var: XmlVar, namespaces: Namespaces): qname = value.qname if hasattr(value, "qname") else var.qname if isinstance(qname, QName): namespaces.add(qname.namespace) sub_element = SubElement(parent, qname) self.render_node(sub_element, value, namespaces) self.set_xsi_type(sub_element, value, var, namespaces) SerializeUtils.set_nil_attribute(sub_element, var.nillable, namespaces)
def render_wildcard_node(self, parent: Element, value: Any, var: XmlVar, namespaces: Namespaces): if value.qname: sub_element = SubElement(parent, value.qname) else: sub_element = parent namespaces.add_all(value.ns_map) SerializeUtils.set_text(sub_element, value.text, namespaces) SerializeUtils.set_tail(sub_element, value.tail, namespaces) SerializeUtils.set_attributes(sub_element, value.attributes, namespaces) for child in value.children: self.render_sub_node(sub_element, child, var, namespaces) SerializeUtils.set_nil_attribute(sub_element, var.nillable, namespaces)
def render_element_node(self, parent: Element, value: Any, var: XmlVar, namespaces: Namespaces): """Render a child element for the given parent according to the field xml metadata.""" if hasattr(value, "qname"): qname = value.qname elif var.is_wildcard: meta = self.context.fetch(value.__class__, QName(parent).namespace) qname = meta.qname else: qname = var.qname namespaces.add(qname.namespace) sub_element = SubElement(parent, qname) self.render_node(sub_element, value, namespaces) self.set_xsi_type(sub_element, value, var, namespaces) SerializeUtils.set_nil_attribute(sub_element, var.nillable, namespaces)
def render_wildcard_node(self, parent: Element, value: Any, var: XmlVar, namespaces: Namespaces): """Render a child element for the given parent according to the wildcard field metadata.""" if value.qname: sub_element = SubElement(parent, value.qname) else: sub_element = parent namespaces.add_all(value.ns_map) SerializeUtils.set_text(sub_element, value.text, namespaces) SerializeUtils.set_tail(sub_element, value.tail, namespaces) SerializeUtils.set_attributes(sub_element, value.attributes, namespaces) for child in value.children: self.render_sub_node(sub_element, child, var, namespaces) SerializeUtils.set_nil_attribute(sub_element, var.nillable, namespaces)
def render_complex_node(self, parent: Element, obj: Any, namespaces: Namespaces): meta = self.context.build(obj.__class__, QName(parent).namespace) for var, value in self.next_value(meta, obj): if value is None: continue elif var.is_attribute: SerializeUtils.set_attribute(parent, var.qname, value, namespaces) elif var.is_attributes: SerializeUtils.set_attributes(parent, value, namespaces) elif var.is_text: namespaces.add(var.qname.namespace) SerializeUtils.set_text(parent, value, namespaces) elif isinstance(value, list): self.render_sub_nodes(parent, value, var, namespaces) else: self.render_sub_node(parent, value, var, namespaces) SerializeUtils.set_nil_attribute(parent, meta.nillable, namespaces)
def test_set_nil_attribute(self): SerializeUtils.set_nil_attribute(self.element, False, self.namespaces) self.assertNotIn(QNames.XSI_NIL, self.element.attrib) self.element.text = "foo" SerializeUtils.set_nil_attribute(self.element, True, self.namespaces) self.assertNotIn(QNames.XSI_NIL, self.element.attrib) self.element.text = None sub_element = SubElement(self.element, "foo") SerializeUtils.set_nil_attribute(self.element, True, self.namespaces) self.assertNotIn(QNames.XSI_NIL, self.element.attrib) self.element.remove(sub_element) SerializeUtils.set_nil_attribute(self.element, True, self.namespaces) self.assertEqual("true", self.element.attrib[QNames.XSI_NIL])