Python Forum

Full Version: xml.etree.ElementTree question.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
tested on python 3.8.2

test.xml contents:
<a>
	<b1>b1 content
		<c1>b1-c1 content</c1>
		<c2>b1-c2 content</c2>
	</b1>
	<b2>b2 content
		<c1>b2-c1 content</c1>
		<c2>b2-c2 content</c2>
	</b2>
</a>
I want to indicate one element's child elements (not include these child elements 's child elements)
I use .getchildren() loke below:

from xml.etree import ElementTree
tree = ElementTree.parse('test.xml')
root = tree.getroot()
root.getchildren()
output:
Quote:<stdin>:1: DeprecationWarning: This method will be removed in future versions. Use 'list(elem)' or iteration over elem instead.
[<Element 'b1' at 0x000000B47A2B78B0>, <Element 'b2' at 0x000000B47A809720>]

If use .iter(), it will output all elements included all hierarchy level elements:
for i in root.iter():
	print(i.text)
output:
Quote:b1 content

b1-c1 content
b1-c2 content
b2 content

b2-c1 content
b2-c2 content

How to indicate element 'b1', 'b2' simple without .getchildren() ?
Huh