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.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With