1

I would like to read an xml file then convert it to a string.

I tried the following:

et = ET.parse('file.xsd')
xml_str = ET.tostring(et, encoding='unicode')

But I'm getting the following error:

LookupError: unknown encoding: unicode
Sam12
  • 1,805
  • 2
  • 15
  • 23
  • Does this answer your question? [Converting a Python XML ElementTree to a String](https://stackoverflow.com/questions/33814607/converting-a-python-xml-elementtree-to-a-string) – ScottC Nov 22 '22 at 10:58
  • What package are you using? Are you using Python 2 or 3 ? – ScottC Nov 22 '22 at 10:59
  • Python 2.7 im using xml.etree.ElementTree – Sam12 Nov 22 '22 at 11:00

1 Answers1

1

Try:

xml_str = ET.tostring(et, encoding='utf-8')

or:

xml_str = ET.tostring(et).decode()

or

import xml.etree.ElementTree as ET
from xml.etree.ElementTree import tostring

tree = ET.parse('file.xsd')
tree = tree.getroot()

xml_str = tostring(tree)
xml_str = xml_str.lower()
tree= ET.fromstring(xml_str)

ScottC
  • 3,941
  • 1
  • 6
  • 20