Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I can't read an XML file

It’s my first time working with XML files, yet I have a problem with a code as simple as:

from xml.etree import ElementTree as ET

tree = ET.parse('some_xml_file.xml')
s = ET.tostring(tree, method = 'xml')
root = tree.getroot()

all I am trying to do here is reading the XML file as a string,
but whenever I try to run this I get an error:

AttributeError: ‘ElementTree’ object has no attribute ‘tag’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have no idea what I did wrong just yet, so I would need any hint
and thanks in advance

>Solution :

You can’t use ET.tostring on the full tree; you can use it on the root element.

from xml.etree import ElementTree as ET

tree = ET.parse('some_xml_file.xml')
s = ET.tostring(tree.getroot(), method='xml')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading