Checking if XML declaration is present
问题 I am trying to check whether an xml file contains the necessary xml declaration ("header"), let's say: <?xml version="1.0" encoding="UTF-8"?> ...rest of xml file... I am using xml ElementTree for reading and getting info out of the file, but it seems to load a file just fine even if it does not have the header. What I tried so far is this: import xml.etree.ElementTree as ET tree = ET.parse(someXmlFile) try: xmlFile = ET.tostring(tree.getroot(), encoding='utf8').decode('utf8') except: sys