Reputation: 123450
I have XML shaped like the following:
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:docs="http://schemas.google.com/docs/2007" xmlns:batch="http://schemas.google.com/gdata/batch"
<entry gd:etag=""HxYZGQVeHyt7ImBr"">
<title>Some document title I wish to find</title>
I have many entry elements, each which contains a title element. I wish to find which entry contains a title element with particular element text.
I can iterate over each item perfectly with the following code:
entry = './/{http://www.w3.org/2005/Atom}entry'
document_nodes = document_feed_xml.findall(entry)
for document_node in document_nodes:
logging.warn('entry item found!')
logging.warn(pretty_print(document_node))
logging.warn('-'*80)
This works, returning:
WARNING:root:--------------------------------------------------------------------------------
WARNING:root:entry item found!
<ns0:entry ns1:etag=""HxdWRh4MGit7ImBr"" xmlns:ns0="http://www.w3.org/2005/Atom" xmlns:ns1="http://schemas.google.com/g/2005">
<ns0:title>
Some document title
</ns0:title>
</ns0:entry>
So now I'd like to look for a 'title' element in this branch of the tree. if I look for:
title = './/{http://www.w3.org/2005/Atom}title'
title_nodes = document_node.findall(title)
for title_node in title_nodes:
logging.warn('yaaay')
logging.warn(title_node.text)
if not title_nodes:
raise ValueError('Could not find any title elements in this entry')
Edit: I originally had 'document_node[0].findall' from some debugging. Removing this, the code above works. This was the cause of the error - thanks the the gent below for spotting this!
This raises the error for no title nodes.
These results seem odd, as: - I can clearly see that element, with that namespace, in the document - I can even run findall() for title directly, using that namespace, and see the results
I've wondered about the possibility of findall() returning objects that are of a different class from it's input, however running 'type' on either object merely returns 'instance' as the type. Quality programming there ElementTree.
Although LXML has better documentation, better xpath support, and better code, for technical reasons, I cannot use LXML, so I am forced to use ElementTree.
Upvotes: 0
Views: 297
Reputation: 32094
The problem is that document_node[0]
in your code already references the title
element, and looking through its children returns nothing.
Upvotes: 1