次のように始まる XML ファイルがあります。
<?xml version="1.0" encoding="utf-8"?>
<Recipe xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
私はそれを読み込んで、修正してから書き戻す必要があります。コード スニペットを次に示します。
from xml.etree import ElementTree
with open('base.xml', 'rt') as f:
tree = ElementTree.parse(f)
recipe = tree.find('')
t = recipe.find('Targets_Params/Target_Table/Target_Name')
t.text = "new Value"
output_file = open('new.xml', 'w' )
output_file.write(ElementTree.tostring(recipe))
output_file.close()
私の問題は、ファイルを書き出すときに最初の行がまったく取得されず、2 行目に次のように出力されることです。
<Recipe>
元の構造を維持しながら、ファイルを読み取り、変更し、書き出すにはどうすればよいですか?