def xml_equal(actual_xml, expected_xml): actual = ET.fromstring(actual_xml) xmlutils.indent(actual) actual_xml = ET.tostring(actual) expected = ET.fromstring(expected_xml) xmlutils.indent(expected) expected_xml = ET.tostring(expected) return actual_xml == expected_xml
def normalized(xml, sort_attrs=True): """ Returns indented XML string with optionally sorted attributes. """ element = xmlutils.fromstring(xml) if sort_attrs: xmlutils.sort_attributes(element) xmlutils.indent(element) return xmlutils.tostring(element)
def format_xml(element, pretty=False): """ Export given DOM element to XML string. :param element: DOM element to export :type element: DOM element :param pretty: whether to make the output more human readable :type pretty: boolean :returns: XML corresponding to `element` content :rtype: string """ if pretty: element = copy.deepcopy(element) xmlutils.indent(element, 0) return etree.tostring(element, encoding='UTF-8')
def assertXMLEqual(self, xml, expectedXML): """ Assert that xml is equivalent to expected xml, ignoring whitespace differences. In case of a mismatch, display normalized xmls to make it easier to find the differences. """ actual = ET.fromstring(xml) xmlutils.indent(actual) actualXML = ET.tostring(actual) expected = ET.fromstring(expectedXML) xmlutils.indent(expected) expectedXML = ET.tostring(expected) self.assertEqual(actualXML, expectedXML, "XMLs are different:\nActual:\n%s\nExpected:\n%s\n" % (actualXML, expectedXML))
def format_xml(element, pretty=False): """ Export given DOM element to XML string. :param element: DOM element to export :type element: DOM element :param pretty: whether to make the output more human readable :type pretty: boolean :returns: XML corresponding to `element` content :rtype: string """ if pretty: element = copy.deepcopy(element) xmlutils.indent(element, 0) # amended version of the implementation of tostring() # found in python 3.6 stream = io.BytesIO() etree.ElementTree(element).write( stream, encoding='utf-8', xml_declaration=True) return stream.getvalue()