Home > Back-end >  I can't read an XML file
I can't read an XML file

Time:09-07

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

CodePudding user response:

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')
  • Related