我们从Python开源项目中,提取了以下17个代码示例,用于说明如何使用xml.etree.ElementTree.ProcessingInstruction()。
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 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 show_xml(document): from xml.etree import ElementTree as XML xml_document = XML.Element("results") legs_xml = XML.SubElement(xml_document, 'legs') for n, leg in enumerate(document['legs'], start=1): leg_xml = XML.SubElement(legs_xml, 'leg', n=str(n)) leg_xml.text = leg teams_xml = XML.SubElement(xml_document, 'teams') for team in document['teams']: team_xml = XML.SubElement(teams_xml, "team") name_xml = XML.SubElement(team_xml, "name") name_xml.text = team['name'] position_xml = XML.SubElement(team_xml, "position") for n, position in enumerate(team['position'], start=1): leg_xml = XML.SubElement(position_xml, "leg", n=str(n)) leg_xml.text = str(position) pi = XML.ProcessingInstruction("xml", 'version="1.0"') XML.dump(pi) XML.dump(xml_document)
def map_node_to_class(self, node): if isinstance(node, BaseET.ElementTree): return nodes.Document elif node.tag == BaseET.ProcessingInstruction: return nodes.ProcessingInstruction elif node.tag == BaseET.Comment: return nodes.Comment elif isinstance(node, ETAttribute): return nodes.Attribute elif isinstance(node, ElementTreeText): if node.is_cdata: return nodes.CDATA else: return nodes.Text elif self._is_node_an_element(node): return nodes.Element raise exceptions.Xml4hImplementationBug( 'Unrecognized type for implementation node: %s' % node)
def test_wrap_node_and_is_type_methods(self): # Wrap root element wrapped_node = self.adapter_class.wrap_node(self.root_elem, self.doc) self.assertEqual(self.root_elem, wrapped_node.impl_node) self.assertEqual('DocRoot', wrapped_node.name) self.assertEqual(self.doc, wrapped_node.impl_document) self.assertTrue(wrapped_node.is_type(xml4h.nodes.ELEMENT_NODE)) self.assertTrue(wrapped_node.is_element) # Wrap a non-root element wrapped_node = self.adapter_class.wrap_node(self.elem3_second, self.doc) self.assertEqual(self.elem3_second, wrapped_node.impl_node) self.assertEqual('ns2:Element3', wrapped_node.name) self.assertEqual('Element4', wrapped_node.parent.name) self.assertTrue(wrapped_node.is_type(xml4h.nodes.ELEMENT_NODE)) self.assertTrue(wrapped_node.is_element) # Test node types wrapped_node = self.adapter_class.wrap_node(self.text_node, self.doc) self.assertIsInstance(wrapped_node, xml4h.nodes.Text) self.assertTrue(wrapped_node.is_type(xml4h.nodes.TEXT_NODE)) self.assertTrue(wrapped_node.is_text) wrapped_node = self.adapter_class.wrap_node(self.cdata_node, self.doc) self.assertIsInstance(wrapped_node, xml4h.nodes.CDATA) self.assertTrue(wrapped_node.is_type(xml4h.nodes.CDATA_NODE)) self.assertTrue(wrapped_node.is_cdata) wrapped_node = self.adapter_class.wrap_node(self.comment_node, self.doc) self.assertIsInstance(wrapped_node, xml4h.nodes.Comment) self.assertTrue(wrapped_node.is_type(xml4h.nodes.COMMENT_NODE)) self.assertTrue(wrapped_node.is_comment) wrapped_node = self.adapter_class.wrap_node( self.instruction_node, self.doc) self.assertIsInstance( wrapped_node, xml4h.nodes.ProcessingInstruction) self.assertTrue( wrapped_node.is_type(xml4h.nodes.PROCESSING_INSTRUCTION_NODE)) self.assertTrue(wrapped_node.is_processing_instruction)
def new_impl_instruction(self, target, data): return self.ET.ProcessingInstruction(target, data)
def get_node_value(self, node): if node.tag == BaseET.ProcessingInstruction: name, target = node.text.split(' ') return target elif node.tag == BaseET.Comment: return node.text elif hasattr(node, 'value'): return node.value else: return node.text
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 """
def setUp(self): if not xml4h.LXMLAdapter.is_available(): self.skipTest("lxml library is not installed") from lxml import etree # Build a DOM using minidom for testing self.root_elem = etree.Element('{urn:test}DocRoot', nsmap={ None: 'urn:test'}) doc = etree.ElementTree(self.root_elem) self.elem1 = etree.Element(u'??1', nsmap={'ns1': 'urn:ns1'}) self.elem1.attrib['a'] = '1' self.elem1.attrib['{urn:ns1}b'] = '2' self.elem2 = etree.Element('Element2') self.elem3 = etree.Element('{urn:ns1}Element3', nsmap={None: 'urn:ns1'}) self.elem4 = etree.Element('{urn:ns1}Element4', nsmap={None: 'urn:ns1'}) self.elem2_second = etree.Element('Element2') self.elem3_second = etree.Element('{urn:ns2}Element3', nsmap={'ns2': 'urn:ns2'}) self.text_node = xml4h.impls.lxml_etree.LXMLText( 'Some text', self.elem1) self.elem1.text = self.text_node.text self.cdata_node = xml4h.impls.lxml_etree.LXMLText( 'Some cdata', self.elem2, is_cdata=True) self.elem2.text = self.cdata_node.text self.comment_node = etree.Comment('A comment') self.instruction_node = etree.ProcessingInstruction( 'pi-target', 'pi-data') self.root_elem.append(self.elem1) self.root_elem.append(self.elem2) self.root_elem.append(self.elem3) self.root_elem.append(self.elem4) self.elem3.append(self.elem2_second) self.elem2_second.append(self.comment_node) self.elem4.append(self.elem3_second) self.elem3_second.append(self.instruction_node) self.doc = doc self.xml4h_doc = xml4h.LXMLAdapter.wrap_document(doc) self.xml4h_root = self.xml4h_doc.root self.xml4h_text = xml4h.LXMLAdapter.wrap_node(self.text_node, self.doc)
def setUp(self): # Use c-based or pure python ElementTree impl based on test class if self.__class__ == TestcElementTreeNodes: if not self.adapter_class.is_available(): self.skipTest( "C-based ElementTree library is not installed" " or is too outdated to be supported by xml4h") import xml.etree.cElementTree as ET else: if not self.adapter_class.is_available(): self.skipTest( "Pure Python ElementTree library is not installed" " or is not too outdated to be supported by xml4h") import xml.etree.ElementTree as ET # Build a DOM using minidom for testing self.root_elem = ET.Element('{urn:test}DocRoot') doc = ET.ElementTree(self.root_elem) self.elem1 = ET.Element(u'??1') self.elem1.attrib['xmlns:ns1'] = 'urn:ns1' self.elem1.attrib['a'] = '1' self.elem1.attrib['{urn:ns1}b'] = '2' self.elem2 = ET.Element('Element2') self.elem3 = ET.Element('{urn:ns1}Element3') self.elem3.attrib['xmlns'] = 'urn:ns1' self.elem4 = ET.Element('{urn:ns1}Element4') self.elem3.attrib['xmlns'] = 'urn:ns1' self.elem2_second = ET.Element('Element2') self.elem3_second = ET.Element('{urn:ns2}Element3') self.elem3_second.attrib['xmlns:ns2'] = 'urn:ns2' self.text_node = xml4h.impls.xml_etree_elementtree.ElementTreeText( 'Some text', self.elem1) self.elem1.text = self.text_node.text self.cdata_node = xml4h.impls.xml_etree_elementtree.ElementTreeText( 'Some cdata', self.elem2, is_cdata=True) self.elem2.text = self.cdata_node.text self.comment_node = ET.Comment('A comment') self.instruction_node = ET.ProcessingInstruction( 'pi-target', 'pi-data') self.root_elem.append(self.elem1) self.root_elem.append(self.elem2) self.root_elem.append(self.elem3) self.root_elem.append(self.elem4) self.elem3.append(self.elem2_second) self.elem2_second.append(self.comment_node) self.elem4.append(self.elem3_second) self.elem3_second.append(self.instruction_node) self.doc = doc self.xml4h_doc = self.adapter_class.wrap_document(doc) self.xml4h_root = self.xml4h_doc.root self.xml4h_text = self.adapter_class.wrap_node( self.text_node, self.doc)