Reputation: 110083
I have a database field which is storing an XML document as Unicode. However, when I fetch the field and try and initiate an lxml
node, I get the following error:
node = etree.fromstring(self.xml)
ValueError: Unicode strings with encoding declaration are not supported. Please use bytes input or XML fragments without declaration.
The text I current have (self.xml
) contains Japanese characters, etc. How would I create the node?
Upvotes: 2
Views: 5853
Reputation: 110083
If you have unicode, you can specify the utf-8 parser for lxml
:
utf8_parser = etree.XMLParser(encoding='utf-8')
node = etree.fromstring(self.xml.encode('utf-8'), parser=utf8_parser)
Upvotes: 6