我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用xml.etree.ElementTree.tostringlist()。
def parseliteral(): """ >>> element = ET.XML("<html><body>text</body></html>") >>> ET.ElementTree(element).write(sys.stdout, encoding='unicode') <html><body>text</body></html> >>> element = ET.fromstring("<html><body>text</body></html>") >>> ET.ElementTree(element).write(sys.stdout, encoding='unicode') <html><body>text</body></html> >>> sequence = ["<html><body>", "text</bo", "dy></html>"] >>> element = ET.fromstringlist(sequence) >>> ET.tostring(element) b'<html><body>text</body></html>' >>> b"".join(ET.tostringlist(element)) b'<html><body>text</body></html>' >>> ET.tostring(element, "ascii") b"<?xml version='1.0' encoding='ascii'?>\\n<html><body>text</body></html>" >>> _, ids = ET.XMLID("<html><body>text</body></html>") >>> len(ids) 0 >>> _, ids = ET.XMLID("<html><body id='body'>text</body></html>") >>> len(ids) 1 >>> ids["body"].tag 'body' """
def parseliteral(): """ >>> element = ET.XML("<html><body>text</body></html>") >>> ET.ElementTree(element).write(sys.stdout) <html><body>text</body></html> >>> element = ET.fromstring("<html><body>text</body></html>") >>> ET.ElementTree(element).write(sys.stdout) <html><body>text</body></html> >>> sequence = ["<html><body>", "text</bo", "dy></html>"] >>> element = ET.fromstringlist(sequence) >>> print ET.tostring(element) <html><body>text</body></html> >>> print "".join(ET.tostringlist(element)) <html><body>text</body></html> >>> ET.tostring(element, "ascii") "<?xml version='1.0' encoding='ascii'?>\\n<html><body>text</body></html>" >>> _, ids = ET.XMLID("<html><body>text</body></html>") >>> len(ids) 0 >>> _, ids = ET.XMLID("<html><body id='body'>text</body></html>") >>> len(ids) 1 >>> ids["body"].tag 'body' """
def _serialize_xml(self, root): """Serialize XML data into string It serializes the dynamic xml created and converts it to a string. This is done before sending the xml to the ILO. :param root: root of the dynamic xml. """ if hasattr(etree, 'tostringlist'): if six.PY3: xml_content_list = [ x.decode("utf-8") for x in etree.tostringlist(root)] else: xml_content_list = etree.tostringlist(root) xml = '\r\n'.join(xml_content_list) + '\r\n' else: if six.PY3: xml_content = etree.tostring(root).decode("utf-8") else: xml_content = etree.tostring(root) xml = xml_content + '\r\n' return xml