Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get XML declaration string with lxml

Tags:

python

xml

lxml

I use lxml to parse XML document How can I get declaration string?

 <?xml version="1.0" encoding="utf-8" ?> 

I want to check if it is present, what encoding it has and what xml version.

like image 315
GhostKU Avatar asked Jun 03 '26 22:06

GhostKU


1 Answers

When parsing your document, the resulting ElementTree object should have a DocInfo object that contains information about the XML or HTML document parsed.

For XML, you may be interested in the xml_version and encoding attributes of this DocInfo:

>>> from lxml import etree
>>> tree = etree.parse('input.xml')
>>> tree.docinfo
<lxml.etree.DocInfo object at 0x7f8111f9ecc0>
>>> tree.docinfo.xml_version
'1.0'
>>> tree.docinfo.encoding
'UTF-8'
like image 95
301_Moved_Permanently Avatar answered Jun 06 '26 13:06

301_Moved_Permanently