473,241 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

ElementTree find with xmlns

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

Oct 13 '07 #1
2 3170
On Oct 12, 11:19 pm, cakebread <cakebr...@gmail.comwrote:
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)

Oct 13 '07 #2
On Oct 12, 8:35 pm, Rajarshi <rajarshi.g...@gmail.comwrote:
>
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>
Pefect, thank you, Rajarshi!

Oct 13 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Matthew Thorley | last post by:
Why does ElementTree.parse convert my xsi to an xmlns? When I do this from elementtree import ElementTree # Sample xml mgac =""" <mgac xmlns="http://www.chpc.utah.edu/~baites/mgacML"...
9
by: Chris Spencer | last post by:
Does anyone know how to make ElementTree preserve namespace prefixes in parsed xml files? The default behavior is to strip a document of all prefixes and then replace them autogenerated prefixes...
1
by: Gerard Flanagan | last post by:
Pseudo-XPath support for ElementTree with the emphasis on 'Pseudo'. http://gflanagan.net/site/python/pagliacci/ElementFilter.html It's an approach suggested by the Specification Pattern eg....
2
by: mirandacascade | last post by:
Situation is this: 1) I have inherited some python code that accepts a string object, the contents of which is an XML document, and produces a data structure that represents some of the content of...
0
by: Mark E. Smith | last post by:
cElementTree cannot hold ElementTree instances. #from elementtree.ElementTree import ElementTree, dump # This works from cElementTree import ElementTree, dump # This does not from...
0
by: Mark | last post by:
-------- Original Message -------- Subject: Using cElementTree and elementtree.ElementInclude Date: Mon, 23 Oct 2006 09:40:24 -0500 From: Mark E. Smith <mark.e.smith@arnold.af.mil> Organization:...
0
by: sndive | last post by:
I have a weid problem. If i do this: import elementtree.ElementTree as ET .... tree = ET.parse("whatever") root = tree.getroot() r = root.find('last') print r return root where last is not an...
1
by: Willemsjunk | last post by:
I tried the tips I found in other posts but I still get 'none' back: import easygui as eg import xml.etree.ElementTree as ET import sys #kml source is: #<?xml version="1.0"...
2
by: Zvi | last post by:
Hi All, Can someone tell me why id the following not working? I have a soap response envelope, for test purpose it's just a string and I create ElementTree from it. Then I try to find Response...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.