[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

finding element by tag in xml

sWrath swrath

2/20/2010 4:27:00 PM

Hi

I am trying to search an element by tag and new in reading a xml file
(in python). I coded this , but it did not work

----------------------------------------------
'''This is to detect the first element and print out all that element
by tag'''


from xml.dom.minidom import parse
from xml.etree.ElementTree import*

file1="book.xml"
tmptree=ElementTree()
tmptree.parse(file1)
items=root.getiterator()


dom = parse(file1)


#Find tag names
for node in items :
if node.tag == 'author': #Get tag
print dom.getElementsByTagName ('book') #Error 1



-----------------------------------------------------------------------------'
2 Questions

1. Why can't I use dom.getElementsByTagName('book') in #Error 1? How
do i print the elements ?
Error- AttributeError: ElementTree instance has no attribute
'getElementsByTagName'



Thanks
John
2 Answers

Jim

2/20/2010 9:41:00 PM

0

On Feb 20, 11:27 am, sWrath swrath <swr...@gmail.com> wrote:
> 2 Questions
>
> 1. Why can't I use dom.getElementsByTagName('book') in #Error 1? How
> do i print the elements ?
> Error- AttributeError: ElementTree instance has no attribute
> 'getElementsByTagName'

I only see one question here.

I think the error is asserting that your instance of the ET class does
not have such a fcn. I also do not see it in the ET documentation.
Perhaps you are looking for the XPath support at http://effbot.org/zone/element...
? Or perhaps you want to make your document dom representation via a
different library?

Jim

Dj Gilcrease

2/20/2010 9:56:00 PM

0

On Sat, Feb 20, 2010 at 9:27 AM, sWrath swrath <swrath@gmail.com> wrote:
> from xml.dom.minidom import parse
> from xml.etree.ElementTree import*
>
> file1="book.xml"
> tmptree=ElementTree()
> tmptree.parse(file1)
> items=root.getiterator()
>
>
> dom = parse(file1)
>
>
> #Find tag names
> for node in items :
>    if node.tag == 'author': #Get tag
>        print dom.getElementsByTagName ('book') #Error 1

You are mixing two different types of xml parsers, either user minidom

for node in dom.getElementsByTagName('author'):
print node.getElementsByTagName('book')

or use etree

for el in items:
if el.tag == 'author':
print el.find('book')

There are a few differences you need to note, the getElementsByTagName
always returns a list even if there is only 1 element and find only
returns the first element found no matter what. If you want to print
all books associated with an author you would need to do something
like

for author in tmptree.getiterator('author'):
for book in author.getiterator('book'):
print book