我们从Python开源项目中,提取了以下20个代码示例,用于说明如何使用xml.etree.ElementTree.PI。
def writefile(): """ >>> elem = ET.Element("tag") >>> elem.text = "text" >>> serialize(elem) '<tag>text</tag>' >>> ET.SubElement(elem, "subtag").text = "subtext" >>> serialize(elem) '<tag>text<subtag>subtext</subtag></tag>' Test tag suppression >>> elem.tag = None >>> serialize(elem) 'text<subtag>subtext</subtag>' >>> elem.insert(0, ET.Comment("comment")) >>> serialize(elem) # assumes 1.3 'text<!--comment--><subtag>subtext</subtag>' >>> elem[0] = ET.PI("key", "value") >>> serialize(elem) 'text<?key value?><subtag>subtext</subtag>' """
def processinginstruction(): """ Test ProcessingInstruction directly >>> ET.tostring(ET.ProcessingInstruction('test', 'instruction')) b'<?test instruction?>' >>> ET.tostring(ET.PI('test', 'instruction')) b'<?test instruction?>' Issue #2746 >>> ET.tostring(ET.PI('test', '<testing&>')) b'<?test <testing&>?>' >>> ET.tostring(ET.PI('test', '<testing&>\xe3'), 'latin1') b"<?xml version='1.0' encoding='latin1'?>\\n<?test <testing&>\\xe3?>" """ # # xinclude tests (samples from appendix C of the xinclude specification)
def bug_200709_element_comment(): """ Not sure if this can be fixed, really (since the serializer needs ET.Comment, not cET.comment). >>> a = ET.Element('a') >>> a.append(ET.Comment('foo')) >>> a[0].tag == ET.Comment True >>> a = ET.Element('a') >>> a.append(ET.PI('foo')) >>> a[0].tag == ET.PI True """
def processinginstruction(): """ Test ProcessingInstruction directly >>> ET.tostring(ET.ProcessingInstruction('test', 'instruction')) '<?test instruction?>' >>> ET.tostring(ET.PI('test', 'instruction')) '<?test instruction?>' Issue #2746 >>> ET.tostring(ET.PI('test', '<testing&>')) '<?test <testing&>?>' >>> ET.tostring(ET.PI('test', u'<testing&>\xe3'), 'latin1') "<?xml version='1.0' encoding='latin1'?>\\n<?test <testing&>\\xe3?>" """ # # xinclude tests (samples from appendix C of the xinclude specification)
def interface(): """ Test element tree interface. >>> element = ET.Element("tag") >>> check_element(element) >>> tree = ET.ElementTree(element) >>> check_element(tree.getroot()) >>> element = ET.Element("t\\xe4g", key="value") >>> tree = ET.ElementTree(element) >>> repr(element) # doctest: +ELLIPSIS "<Element 't\\xe4g' at 0x...>" >>> element = ET.Element("tag", key="value") Make sure all standard element methods exist. >>> check_method(element.append) >>> check_method(element.extend) >>> check_method(element.insert) >>> check_method(element.remove) >>> check_method(element.getchildren) >>> check_method(element.find) >>> check_method(element.iterfind) >>> check_method(element.findall) >>> check_method(element.findtext) >>> check_method(element.clear) >>> check_method(element.get) >>> check_method(element.set) >>> check_method(element.keys) >>> check_method(element.items) >>> check_method(element.iter) >>> check_method(element.itertext) >>> check_method(element.getiterator) These methods return an iterable. See bug 6472. >>> check_method(element.iter("tag").__next__) >>> check_method(element.iterfind("tag").__next__) >>> check_method(element.iterfind("*").__next__) >>> check_method(tree.iter("tag").__next__) >>> check_method(tree.iterfind("tag").__next__) >>> check_method(tree.iterfind("*").__next__) These aliases are provided: >>> assert ET.XML == ET.fromstring >>> assert ET.PI == ET.ProcessingInstruction >>> assert ET.XMLParser == ET.XMLTreeBuilder """
def interface(): r""" Test element tree interface. >>> element = ET.Element("tag") >>> check_element(element) >>> tree = ET.ElementTree(element) >>> check_element(tree.getroot()) >>> element = ET.Element("t\xe4g", key="value") >>> tree = ET.ElementTree(element) >>> repr(element) # doctest: +ELLIPSIS "<Element 't\\xe4g' at 0x...>" >>> element = ET.Element("tag", key="value") Make sure all standard element methods exist. >>> check_method(element.append) >>> check_method(element.extend) >>> check_method(element.insert) >>> check_method(element.remove) >>> check_method(element.getchildren) >>> check_method(element.find) >>> check_method(element.iterfind) >>> check_method(element.findall) >>> check_method(element.findtext) >>> check_method(element.clear) >>> check_method(element.get) >>> check_method(element.set) >>> check_method(element.keys) >>> check_method(element.items) >>> check_method(element.iter) >>> check_method(element.itertext) >>> check_method(element.getiterator) These methods return an iterable. See bug 6472. >>> check_method(element.iter("tag").next) >>> check_method(element.iterfind("tag").next) >>> check_method(element.iterfind("*").next) >>> check_method(tree.iter("tag").next) >>> check_method(tree.iterfind("tag").next) >>> check_method(tree.iterfind("*").next) These aliases are provided: >>> assert ET.XML == ET.fromstring >>> assert ET.PI == ET.ProcessingInstruction >>> assert ET.XMLParser == ET.XMLTreeBuilder """