How to read commented text from XML file in python
Asked Answered
O

1

2

I am able to read the xml file in a using 'import xml.etree.ElementTree as et'. But my problem is to read the commented text given in the data file, how to read this: For example in the below xml, I want to read BaseVehicle is 1997 Cadillac Catera

<App action="A" id="1">
    <BaseVehicle id="8559"/>
    <!--  1997 Cadillac Catera  -->
    <Qty>1</Qty>
    <PartType id="4472"/>
    <!--  Electrical/Headlight/Switch  -->
    <Part>SW1406</Part>
</App>
Outdistance answered 31/12, 2019 at 11:55 Comment(2)
check this : bugs.python.org/issue8277Mentholated
Thank you for the help, but I am getting error - "ImportError: cannot import name 'XMLTreeBuilder' from 'xml.etree.ElementTree" Also When I searched and tried to use the below code:Outdistance
D
9

The standard behaviour of ElementTree is to ignore comments. However, comments can be preserved by using a custom parser object. This has become easier in Python 3.8, where the xml.etree.ElementTree.TreeBuilder target can be configured to process comment events in order to include them in the generated tree.

from xml.etree import ElementTree as ET

parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=True)) # Python 3.8
tree = ET.parse("app.xml", parser)

# Get the comment nodes
for node in tree.iter():
    if "function Comment" in str(node.tag): 
        print(node.text)

Output:

1997 Cadillac Catera
Electrical/Headlight/Switch

With older versions of Python, some more code is needed. See Faithfully Preserve Comments in Parsed XML.

Doglike answered 2/1, 2020 at 10:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.