finding element by tag in xml

S

sWrath swrath

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
 
J

Jim

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-xpath.htm
? Or perhaps you want to make your document dom representation via a
different library?

Jim
 
D

Dj Gilcrease

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top