xml.etree.ElementTree question. - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: xml.etree.ElementTree question. (/thread-30152.html) |
xml.etree.ElementTree question. - water - Oct-09-2020 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. 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 How to indicate element 'b1', 'b2' simple without .getchildren() ? |