Keep Existing Namespaces when overwriting XML file with ElementTree and Python
问题 I have an XML file in the following format <?xml version="1.0" encoding="utf-8"?> <foo> <bar> <bat>1</bat> </bar> <a> <b xmlns="urn:schemas-microsoft-com:asm.v1"> <c>1</c> </b> </a> </foo> I want to change the value of bat to '2' and change the file to this: <?xml version="1.0" encoding="utf-8"?> <foo> <bar> <bat>2</bat> </bar> <a> <b xmlns="urn:schemas-microsoft-com:asm.v1"> <c>1</c> </b> </a> </foo> I open this file by doing this tree = ET.parse(filePath) root = tree.getroot() I then change