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()
Then I change the value of bat to "2" and save the file as follows:
tree.write(filePath, "utf-8", True, None, "xml")
The bat value is successfully changed to 2, but the XML file now looks like this.
<?xml version="1.0" encoding="utf-8"?>
<foo xmlns:ns0="urn:schemas-microsoft-com:asm.v1">
<bar>
<bat>2</bat>
</bar>
<a>
<ns0:b>
<ns0:c>1</ns0:c>
</ns0:b>
</a>
</foo>
To fix a problem with the namespace named ns0 before analyzing the document
follow these steps:ET.register_namespace('', "urn:schemas-microsoft-com:asm.v1")
This will get rid of the ns0 namespace, but now the xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<foo xmlns="urn:schemas-microsoft-com:asm.v1">
<bar>
<bat>2</bat>
</bar>
<a>
<b>
<c>1</c>
</b>
</a>
</foo>
What should I do to get the result I need?