我正在编写一个脚本来查找,例如:Asset_ID,在XML中多次出现,并将Asset_ID替换为作为参数接收的值。这只是一个例子。我必须发送许多参数并替换XML中的每个值并保存XML以供进一步处理。
XML看起来像这样
<ADI>
<Metadata>
<AMS Asset_Class="package" Asset_ID="PVOD0000000010200163" Asset_Name="Test" Creation_Date="2010-01-11" Description="Simple" Product="HOD" Provider="AssetSource1" Provider_ID="Model" Version_Major="34" Version_Minor="1"/>
<App_Data App="MOD" Name="Test1" Value="Test12"/>
<App_Data App="MOD" Name="Test13" Value="TTTT"/>
</Metadata>
<Asset>
<Metadata>
<AMS Asset_Class="title" Asset_ID="PVOD1000000010200163" Asset_Name="Sample" Creation_Date="2010-01-11" Description="Docs" Product="You" Provider="Me" Provider_ID="p2" Version_Major="34" Version_Minor="1"/>
</Metadata>
<Asset>
</ADI>
我写的代码无法找到确切的值。有人可以帮助我吗?
import xml.etree.ElementTree as ET
number = 'TITL1000000000'
tree = ET.parse('C:\\Work\\Scripts\\Child9.xml')
for element in tree.findall('.//AMS'):
# Get the prefix alpha characters used in each section
prefix = element.get('Asset_ID')[:4]
# Set the Asset_ID to the prefix + the ID
element.set('Asset_ID', '{asset_number}'.format(asset_number=number))
# Write Output
tree.write('{asset_number}.xml'.format(asset_number=number))