ElementTree find with xmlns

Rajarshi rajarshi.guha at gmail.com
Fri Oct 12 23:35:10 EDT 2007


On Oct 12, 11:19 pm, cakebread <cakebr... at gmail.com> wrote:
> I'm having problems parsing a file:
>
> >>> tree = ElementTree.fromstring("""<html xmlns="http://www.w3.org/1999/xhtml" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:doap="http://usefulinc.com/ns/doap#">
>
> <body>Hello world</body>
> </html>""")
>
> >>> print tree.find('body')
>
> None
>
> The above works fine with the first element being a simple <html>, but
> not when I have all the xmlns's.
>
> Thanks,
> Rob


You have to prefix the element name with its namespace. The following
will work

>>> tree.find('{http://www.w3.org/1999/xhtml}body')
<Element {http://www.w3.org/1999/xhtml}body at 779d28>

(Python 2.5, OS X 10.4.10)




More information about the Python-list mailing list