示例#1
0
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
示例#2
0
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)
示例#3
0
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')
示例#4
0
文件: vmxml.py 项目: rollandf/vdsm
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')
示例#5
0
    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))
示例#6
0
文件: testlib.py 项目: EdDev/vdsm
    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))
示例#7
0
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()