0

The question is simple:

How can I put xml comments using import xml.etree.cElementTree as ET library.

Python version: 3

Edit:

tree = ET.ElementTree(papinhio_player)
            comment = "Αρχείο προσπελάσιμο από την εφαρμογή Papinhio Player.\nΠαρακαλώ μην το τροποποιήσετε και μην το σβήσετε για την ορθή λειτουργεία του προγράμματος."
            tree.Comment(comment)

Output:

Traceback (most recent call last):
  File "main.py", line 104, in <module>
    start = StartProgram()
  File "main.py", line 14, in __init__
    self.load_xml_data()
  File "main.py", line 98, in load_xml_data
    tree.Comment(comment)
AttributeError: 'ElementTree' object has no attribute 'Comment'

Edit2:

object_methods = [method_name for method_name in dir(ET) if callable(getattr(ET, method_name))]
            print(object_methods)

Output:

['Comment', 'Element', 'ElementTree', 'PI', 'ParseError', 'ProcessingInstruction', 'QName', 'SubElement', 'TreeBuilder', 'XML', 'XMLID', 'XMLParser', 'XMLPullParser', 'dump', 'fromstring', 'fromstringlist', 'iselement', 'iterparse', 'parse', 'register_namespace', 'tostring', 'tostringlist']

Edit3:

papinhio_player = ET.Element("papinhio_player")
            comment = ET.Comment("Αρχείο προσπελάσιμο από την εφαρμογή Papinhio Player.Παρακαλώ μην το τροποποιήσετε και μην το σβήσετε για την ορθή λειτουργεία του προγράμματος.")
            papinhio_player.append(comment)

works! But it put the comment inside the first node. I want the comment to be put after xml declaration.

I think that's ok for now! Thanks a lot all.

mzjn
  • 48,958
  • 13
  • 128
  • 248
Chris P
  • 2,059
  • 4
  • 34
  • 68
  • The same question as https://stackoverflow.com/q/56886619/407651. The answer does not solve the problem, unfortunately. – mzjn Apr 27 '20 at 04:24
  • I have posted an answer to the linked question that solves the problem by using lxml. – mzjn Apr 27 '20 at 11:27

0 Answers0