0

I have a xml file containing one interesting comment, and I would like to parse it.

Here I discovered how I can handle comments, but I don't know how to use them from my main app.

#!/usr/bin/python3

import xml.etree.ElementTree as ET

with open('xml_with_comments.xml', 'w') as f:
    f.write('''<?xml version="1.0" encoding="UTF-8"?>
    <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <blah>node 1</blah>
        <!-- secret_content: Hello! -->
        <blah>node 2</blah>
        <!-- A standard comment -->
        <blah>node 3</blah>
    </root>
    ''')

class TreeBuilderWithComments(ET.TreeBuilder):
    def comment(self, data):
        if data.startswith(' secret_content: '):
            self.start(ET.Comment, {})
            self.data(data)
            self.end(ET.Comment)
            print('Secret content from TreeBuilderWithComments: ' + data[17:-1])

root = ET.parse('xml_with_comments.xml', parser=ET.XMLParser(target=TreeBuilderWithComments())).getroot()
for blah in root.findall('blah'):
    print(blah.text)

This outputs:

Secret content from TreeBuilderWithComments: Hello!
node 1
node 2
node 3

Now I would like to do something like print(root.get_secret_content()), which should print the first comment of the file begining by ' secret_content: '.

1 Answer 1

1

You make an instance of TreeBuilderWithComments() inside the call to ET.parse, if you keep a reference to it you can use that instance to get the secret content:

 # do this first.
comment_handler = TreeBuilderWithComments()

root = ET.parse('xml_with_comments.xml',
                parser=ET.XMLParser(target=comment_handler)
               ).getroot()                # ^^ used here!

for blah in root.findall('blah'):
    print(blah.text)

Then you can implement .get_secret_content to your TreeBuilderWithComments class and use it on the comment_handler instance.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I also modified TreeBuilderWithComments.comment(): def comment(self, data): [\n] SECRET_CONTENT_KEY = 'secret_content: ' [\n] data = data.strip() [\n] if data.startswith(SECRET_CONTENT_KEY): [\n] self.secret_content = data[len(SECRET_CONTENT_KEY):]. So get_secret_content() is a simple getter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.