[Tutor] small ElementTree problem

Alex Hall mehgcap at gmail.com
Sat Jan 29 01:18:02 CET 2011


On 1/28/11, Stefan Behnel <stefan_ml at behnel.de> wrote:
> Alex Hall, 28.01.2011 14:25:
>> On 1/28/11, Stefan Behnel wrote:
>>> Alex Hall, 28.01.2011 14:09:
>>>> On 1/28/11, Stefan Behnel wrote:
>>>>> Alex Hall, 27.01.2011 23:23:
>>>>>>     self.id=root.find("id").text
>>>>>> self.name=root.find("name).text
>>>>>
>>>>> There's a findtext() method on Elements for this purpose.
>>>>>
>>>> I thought that was used to search for the text of an element? I want
>>>> to get the text, whatever it may be, not search for it. Or am I
>>>> misunderstanding the function?
>>>
>>> What do you think 'find()' does? Use the Source, Luke. ;)
>> Here is what I am thinking:
>> element.find("tagname"): returns an element with the tag name, the
>> first element with that name to be found. You can then use the usual
>> properties and methods on this element.
>> element.findtext("text"): returns the first element found that has a
>> value of "text". Take this example:
>> <root>
>> <a>some text</a>
>> </root>
>> Now you get the root, then call:
>> root.find("a") #returns the "a" element
>> root.findtext("some text") #also returns the "a" element
>
> Ah, ok, then you should read the documentation:
>
> http://docs.python.org/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.findtext
I had been reading documentation, but not that page. Thanks.
>
> findtext() does what find() does, except that it returns the text value of
> the Element instead of the Element itself.
>
> It basically spells out to "find text of element matching(path)".
Well, this will make life easier...
>
> Stefan
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehgcap at gmail.com; http://www.facebook.com/mehgcap


More information about the Tutor mailing list