KD01
KD01

Reputation: 165

How to read commented text from XML file in python

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>

Upvotes: 2

Views: 3641

Answers (1)

mzjn
mzjn

Reputation: 50977

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.

Upvotes: 9

Related Questions