I am converting an xml file to string, then converting it back to xml. I want the original xml and the one at the end to be exactly identical. But this is not the case for two reasons: 1. the keys order is changed, 2. there is a space at the end before the last slash:
# original xml: <W Duration="180" PowerLow="0.45449999" PowerHigh="0.75449997"/> # after str conversion: <W Duration="180" PowerHigh="0.75449997" PowerLow="0.45449999" />
Code:
# read xml and convert to str tree = ET.parse(xml_file_path) root = tree.getroot() xmlstr = ET.tostring(root, encoding='utf8', method='xml') # str > xml and save to file tree = ET.ElementTree(ET.fromstring(xmlstr)) filename = os.path.join(TMP_DIR, next(tempfile._get_candidate_names()) + '.xml' ) tree.write(open(filename, 'w'), encoding='unicode')
I need the two files to be exactly identical, how can I do that?
Thanks!