XML: minidom and problem with getElementsByTagName()

Jaros³aw Zabie³³o (delete .PL) webmaster at apologetyka.com.pl
Fri May 3 17:06:38 EDT 2002


I have problems with parsing the following easy xml tree using Python.
The problem is minidom (I am using) go to deep for children nodes. My
example xml is:

<main>
	<book>
		<nr>first</nr>
		<chapter>
			<nr>a</nr>
			<txt>blah</txt>
		</chapter>
		<chapter>
			<nr>b</nr>
			<txt>blah</txt>
		</chapter>
	</book>
	<book>
		<nr>second</nr>
		<chapter>
			<nr>a</nr>
			<txt>blah</txt>
		</chapter>
		<chapter>
			<nr>b</nr>
			<txt>blah</txt>
		</chapter>
	</book>
</main>

I wrote:

from xml.dom import minidom
doc = minidom.parse('myfilename.xml')
nodeBook = doc.getElementsByTagName('book')
for nodeNr in nodeBook.getElementsByTagName('nr'):
	print nodeNr.firstChild.data

I got 'first', 'a'. I wanted: 'first', 'second'. It seems to work as
XPath for '//nr' but I want only 'nr'. It means, I want to get all
nodes at the current level NOT to every descendants. :-(

Can I make minidom for getting childeren nodes without recursion? Do
getElementsByTagName() method works always with recursion???

--
Jarosław Zabiełło (UIN: 6712522)
URL: http://3585753410/~zbiru





More information about the Python-list mailing list