Python Forum
xml.etree.ElementTree question.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
xml.etree.ElementTree question.
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  xml file editing with lxml.etree FlavioBueno 2 631 Jun-09-2023, 02:00 PM
Last Post: FlavioBueno
  [SOLVED] [ElementTree] Grab text in attributes? Winfried 3 1,583 May-27-2022, 04:59 PM
Last Post: Winfried
  [ElementTree] Insert big block of HTML? Winfried 0 1,150 May-12-2022, 07:08 AM
Last Post: Winfried
  ElementTree get attribute value part of string paulo79 1 2,040 Apr-05-2022, 09:13 PM
Last Post: deanhystad
  xml.etree.ElementTree extract string values matthias100 2 4,856 Jul-12-2020, 06:02 PM
Last Post: snippsat
  [pykml] "AttributeError: 'lxml.etree._ElementTree' object has no attribute 'Document' Winfried 3 6,474 May-26-2020, 09:30 PM
Last Post: Winfried
  Python 3 Elementtree and Comment First gw1500se 3 3,661 May-25-2020, 09:02 PM
Last Post: gw1500se
  Write the XML file from elementtree with hexa decimal encoding Dillibabu 4 3,422 Dec-24-2019, 10:10 AM
Last Post: Dillibabu
  looking for sample py to read from txt file into XML using lxml import etree venkat18 3 2,930 Jun-02-2019, 04:34 AM
Last Post: venkat18
  Please help me error "AttributeError: 'module' object has no attribute 'ElementTree'" mattroi261192 4 6,917 Nov-11-2018, 05:01 AM
Last Post: mattroi261192

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020