def test_DOM_node_to_XML(self): XML_declaration_line = "<?xml version='1.0' encoding='utf8'?>\n" expected = "<root><x><a>2</a><b>3</b></x></root>" test_root_node = ET.Element("root") test_child_node = ET.SubElement(test_root_node, "x") test_grand_child_node_1 = ET.SubElement(test_child_node, "a") test_grand_child_node_1.text = "2" test_grand_child_node_1 = ET.SubElement(test_child_node, "b") test_grand_child_node_1.text = "3" self.assertEqual(expected, xmlutils.DOM_node_to_XML(test_root_node, False)) self.assertEqual(XML_declaration_line + expected, xmlutils.DOM_node_to_XML(test_root_node))
def test_annotate_with_XMLNS_with_Element_as_input(self): expected = '<root xmlns:p="http://test.com/schemas/f.xsd">' + \ '<p:c1><p:gc1>test</p:gc1></p:c1></root>' root_node = ET.Element("root") c1_node = ET.SubElement(root_node, "c1") gc1_node = ET.SubElement(c1_node, "gc1") gc1_node.text = "test" xmlutils.annotate_with_XMLNS(root_node, 'p', 'http://test.com/schemas/f.xsd') self.assertEqual(expected, xmlutils.DOM_node_to_XML(root_node, False))
def to_XML(self, xml_declaration=True, xmlns=True): """ Dumps object fields to an XML-formatted string. The 'xml_declaration' switch enables printing of a leading standard XML line containing XML version and encoding. The 'xmlns' switch enables printing of qualified XMLNS prefixes. :param XML_declaration: if ``True`` (default) prints a leading XML declaration line :type XML_declaration: bool :param xmlns: if ``True`` (default) prints full XMLNS prefixes :type xmlns: bool :returns: an XML-formatted string """ root_node = self._to_DOM() if xmlns: xmlutils.annotate_with_XMLNS(root_node, OZONE_XMLNS_PREFIX, OZONE_XMLNS_URL) return xmlutils.DOM_node_to_XML(root_node, xml_declaration)