我们从Python开源项目中,提取了以下33个代码示例,用于说明如何使用xml.etree.ElementTree.Comment()。
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 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 __init__(self, nml_boiler=False): self.comp_type = Element('ComponentType') self.dynamics = Element('Dynamics') if nml_boiler: self.comp_type.set('extends', 'baseIonChannel') self.comp_type.append( Comment('The defs below are hardcoded for testing purposes!')) SubElement(self.comp_type, 'Constant', attrib={ 'dimension': 'voltage', 'name': 'MV', 'value': '1mV'}) SubElement(self.comp_type, 'Constant', attrib={ 'dimension': 'time', 'name': 'MS', 'value': '1ms'}) SubElement(self.comp_type, 'Requirement', attrib={ 'name': 'v', 'dimension': 'voltage'}) self.comp_type.append(Comment('End of hardcoded defs!'))
def add_entity_to_doc(doc_element, entity, n): entityname = ET.SubElement(doc_element, "entity") id = ET.SubElement(entityname, "id") id.text = "t" + str(n) entityname.append(ET.Comment(entity.text)) #id.text = str(len(doc_element.findall("entity"))) span = ET.SubElement(entityname, "span") span.text = str(entity.dstart)+","+str(entity.dend) type = ET.SubElement(entityname, "type") type.texts = "EVENT" propertiesname = ET.SubElement(entityname, "properties") classs = ET.SubElement(propertiesname, "Class") value = ET.SubElement(propertiesname, "Value") #### propertiesname = ET.SubElement(entityname, "properties") #XML classs = ET.SubElement(propertiesname, "Class") # XML classs.text = "asdasd"#entity.tag ## return doc_element
def add_relation_to_doc(doc_element, relation): relationname = ET.SubElement(doc_element, "relation") #XML entity id = ET.SubElement(relationname, "id") #XML id id.text = convert_id(relation.pid) # id.append(ET.Comment(relation.entities[0].text + "+" + relation.entities[1].text)) #id.text = str(len(doc_element.findall("entity"))) esta so comentado type = ET.SubElement(relationname, "type") #XML type.text = "TLINK" # entity1 = ET.SubElement(relationname, "parentsType") # entity1.text = "TemporalRelations" propertiesname = ET.SubElement(relationname, "properties") #XML source = ET.SubElement(propertiesname, "Source") # XML source.text = convert_id(relation.entities[0].eid) propertiesname.append(ET.Comment("Source:{}".format(relation.entities[0].text))) t = ET.SubElement(propertiesname, "Type") # XML t.text = "CONTAINS" target = ET.SubElement(propertiesname, "Target") # XML target.text = convert_id(relation.entities[1].eid) propertiesname.append(ET.Comment("Target:{}".format(relation.entities[1].text))) return doc_element
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 summarize(elem): if elem.tag == ET.Comment: return "<Comment>" return elem.tag
def bug_200709_iter_comment(): """ >>> a = ET.Element('a') >>> b = ET.SubElement(a, 'b') >>> comment_b = ET.Comment("TEST-b") >>> b.append(comment_b) >>> summarize_list(a.iter(ET.Comment)) ['<Comment>'] """ # -------------------------------------------------------------------- # reported on bugs.python.org
def bug_200709_iter_comment(): """ >>> a = ET.Element('a') >>> b = ET.SubElement(a, 'b') >>> comment_b = ET.Comment("TEST-b") >>> b.append(comment_b) >>> summarize_list(a.iter(ET.Comment)) ['<Comment>'] """
def set_text(element, content): content = escape(content, entities={'\r\n': '<br />'}) # retain html tags in text content = content.replace("\n", "<br />") # replace new line for *nix system content = content.replace("<br />", "<br />\n") # add the line break in source to make it readable # trick to add CDATA for element tree lib element.append(Comment(' --><![CDATA[' + content.replace(']]>', ']]]]><![CDATA[>') + ']]><!-- '))
def comment(self, data): self.start(XmlET.Comment, {}) self.data(data) self.end(XmlET.Comment)
def comment(self, data): self.start(ET.Comment, {}) self.data(data) self.end(ET.Comment)
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_comment(self, text): return self.ET.Comment(text)
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 save(self): assert self.m_filename if self.g_latex_radio.get_active(): format = "latex" else: format = "html" doc = et.Element("sheet", fileformat_version=self.current_fileformat_version, creator="GNU Solfege", app_version=buildinfo.VERSION_STRING) doc.append(et.Comment("")) et.SubElement(doc, "title").text = self.g_title.get_text() et.SubElement(doc, "output_format").text = format for sect in self.m_sections: s = et.Element("section") et.SubElement(s, "title").text = sect['title'] et.SubElement(s, "filename").text = sect['filename'] # only the harmonic and melodic intervals have this variable: if 'intervals' in sect: et.SubElement(s, 'intervals').text = "%s" % sect['intervals'] # We do save 'count' because this variable say how many questions # we want to generate, and len(questions) will just say how many # are generated now. et.SubElement(s, "count").text = "%i" % sect['count'] et.SubElement(s, "line_len").text = "%i" % sect['line_len'] et.SubElement(s, "qtype").text = "%i" % sect['qtype'] for qdict in sect['questions']: q = et.SubElement(s, "question") t = et.SubElement(q, "teachers") et.SubElement(t, "name").text = qdict['answer']['name'] et.SubElement(t, "music").text = qdict['answer']['music'] t = et.SubElement(q, "students") et.SubElement(t, "name").text = qdict['question']['name'] et.SubElement(t, "music").text = qdict['question']['music'] doc.append(s) tree = et.ElementTree(doc) f = open(self.m_filename, 'w') print >> f, '<?xml version="1.0" encoding="utf-8"?>' tree.write(f, encoding="utf-8") f.close() self.m_changed = False
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)