Sam12
Sam12

Reputation: 1805

Reading an xml file and converting it to a string

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

Upvotes: 1

Views: 178

Answers (1)

ScottC
ScottC

Reputation: 4105

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)

Upvotes: 1

Related Questions