David542
David542

Reputation: 110083

Creating xml node from Unicode string (encoding declaration not supported)?

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

Answers (1)

David542
David542

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

Related Questions