我们从Python开源项目中,提取了以下28个代码示例,用于说明如何使用xml.etree.ElementTree.findtext()。
def test_find_simple(self): e = ET.XML(SAMPLE_XML) self.assertEqual(e.find('tag').tag, 'tag') self.assertEqual(e.find('section/tag').tag, 'tag') self.assertEqual(e.find('./tag').tag, 'tag') e[2] = ET.XML(SAMPLE_SECTION) self.assertEqual(e.find('section/nexttag').tag, 'nexttag') self.assertEqual(e.findtext('./tag'), 'text') self.assertEqual(e.findtext('section/tag'), 'subtext') # section/nexttag is found but has no text self.assertEqual(e.findtext('section/nexttag'), '') self.assertEqual(e.findtext('section/nexttag', 'default'), '') # tog doesn't exist and 'default' kicks in self.assertIsNone(e.findtext('tog')) self.assertEqual(e.findtext('tog', 'default'), 'default') # Issue #16922 self.assertEqual(ET.XML('<tag><empty /></tag>').findtext('empty'), '')
def simplefind(): """ Test find methods using the elementpath fallback. >>> from xml.etree import ElementTree >>> CurrentElementPath = ElementTree.ElementPath >>> ElementTree.ElementPath = ElementTree._SimpleElementPath() >>> elem = ElementTree.XML(SAMPLE_XML) >>> elem.find("tag").tag 'tag' >>> ElementTree.ElementTree(elem).find("tag").tag 'tag' >>> elem.findtext("tag") 'text' >>> elem.findtext("tog") >>> elem.findtext("tog", "default") 'default' >>> ElementTree.ElementTree(elem).findtext("tag") 'text' >>> summarize_list(elem.findall("tag")) ['tag', 'tag'] >>> summarize_list(elem.findall(".//tag")) ['tag', 'tag', 'tag'] Path syntax doesn't work in this case. >>> elem.find("section/tag") >>> elem.findtext("section/tag") >>> summarize_list(elem.findall("section/tag")) [] >>> ElementTree.ElementPath = CurrentElementPath """
def bug_xmltoolkit25(): """ typo in ElementTree.findtext >>> elem = ET.XML(SAMPLE_XML) >>> tree = ET.ElementTree(elem) >>> tree.findtext("tag") 'text' >>> tree.findtext("section/tag") 'subtext' """
def test_bug_xmltoolkit25(self): # typo in ElementTree.findtext elem = ET.XML(SAMPLE_XML) tree = ET.ElementTree(elem) self.assertEqual(tree.findtext("tag"), 'text') self.assertEqual(tree.findtext("section/tag"), 'subtext')
def test_find_through_ElementTree(self): e = ET.XML(SAMPLE_XML) self.assertEqual(ET.ElementTree(e).find('tag').tag, 'tag') self.assertEqual(ET.ElementTree(e).findtext('tag'), 'text') self.assertEqual(summarize_list(ET.ElementTree(e).findall('tag')), ['tag'] * 2) # this produces a warning self.assertEqual(summarize_list(ET.ElementTree(e).findall('//tag')), ['tag'] * 3)
def test_issue14818(self): x = ET.XML("<a>foo</a>") self.assertEqual(x.find('a', None), x.find(path='a', namespaces=None)) self.assertEqual(x.findtext('a', None, None), x.findtext(path='a', default=None, namespaces=None)) self.assertEqual(x.findall('a', None), x.findall(path='a', namespaces=None)) self.assertEqual(list(x.iterfind('a', None)), list(x.iterfind(path='a', namespaces=None))) self.assertEqual(ET.Element('a').attrib, {}) elements = [ ET.Element('a', dict(href="#", id="foo")), ET.Element('a', attrib=dict(href="#", id="foo")), ET.Element('a', dict(href="#"), id="foo"), ET.Element('a', href="#", id="foo"), ET.Element('a', dict(href="#", id="foo"), href="#", id="foo"), ] for e in elements: self.assertEqual(e.tag, 'a') self.assertEqual(e.attrib, dict(href="#", id="foo")) e2 = ET.SubElement(elements[0], 'foobar', attrib={'key1': 'value1'}) self.assertEqual(e2.attrib['key1'], 'value1') with self.assertRaisesRegex(TypeError, 'must be dict, not str'): ET.Element('a', "I'm not a dict") with self.assertRaisesRegex(TypeError, 'must be dict, not str'): ET.Element('a', attrib="I'm not a dict") # --------------------------------------------------------------------
def test_findtext_with_mutating(self): e = ET.Element('foo') e.extend([ET.Element('bar')]) e.findtext(MutatingElementPath(e, 'x'))
def test_findtext_with_error(self): e = ET.Element('foo') e.extend([ET.Element('bar')]) try: e.findtext(BadElementPath('x')) except ZeroDivisionError: pass
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 """