Home > database >  Reading an xml file and converting it to a string
Reading an xml file and converting it to a string

Time:11-22

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

CodePudding user response:

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)

  •  Tags:  
  • xml
  • Related