I can't read an XML file

Question:

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’

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

Asked By: Ahmad Hamdy

||

Answers:

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')
Answered By: AKX
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.