我有一个大的xml文件,我想根据id(这里的id是唯一的)把这个xml文件分成多个文件。
我的文件如下所示
main.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tal xmlns="http://hello.com" schemaVersion="5.0.0" refSchemaFile="tal.xsd" status="Executable">
<ExecProperties supportsParallelMostFlash="false"/>
<talLine id="tl_1" status="Executable" baseVariant="DKOMBI8" diagAddress="60">
<blFlash status="Executable">
<blFlashTA status="Executable">
<sgbmid>
<processClass>BTLD</processClass>
<id>00007732</id>
<mainVersion>2</mainVersion>
<subVersion>3</subVersion>
<patchVersion>11</patchVersion>
</sgbmid>
</blFlashTA>
<blFlashTA status="Executable">
<sgbmid>
<processClass>FLSL</processClass>
<id>00007735</id>
<mainVersion>2</mainVersion>
<subVersion>3</subVersion>
<patchVersion>11</patchVersion>
</sgbmid>
</blFlashTA>
</blFlash>
</talLine>
<talLine id="tl_2" status="Executable" baseVariant="DKOMBI8" diagAddress="60">
<swDeploy status="Executable">
<swDeployTA status="Executable">
<sgbmid>
<processClass>SWFL</processClass>
<id>00007736</id>
<mainVersion>2</mainVersion>
<subVersion>3</subVersion>
<patchVersion>11</patchVersion>
</sgbmid>
</swDeployTA>
<swDeployTA status="Executable">
<sgbmid>
<processClass>SWFL</processClass>
<id>00007bfc</id>
<mainVersion>2</mainVersion>
<subVersion>3</subVersion>
<patchVersion>11</patchVersion>
</sgbmid>
</swDeployTA>
</swDeploy>
</talLine>
<talLine id="tl_3" status="Executable" baseVariant="DKOMBI8" diagAddress="60">
<cdDeploy status="Executable">
<cdDeployTA status="Executable">
<sgbmid>
<processClass>CAFD</processClass>
<id>00006d4e</id>
<mainVersion>0</mainVersion>
<subVersion>4</subVersion>
<patchVersion>11</patchVersion>
</sgbmid>
</cdDeployTA>
</cdDeploy>
</talLine>
<executionTime actualEndTime="0" actualStartTime="0" plannedEndTime="0" plannedStartTime="0"/>
<installedECUList_Ist/>
<installedECUList_Soll/>
我需要所有的 "id "数据都在一个文件中,并带有页眉和页脚(你可以观察下面的文件),上面是例子文件。我需要一个像下面这样的文件
1.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tal xmlns="http://hello.com" schemaVersion="5.0.0" refSchemaFile="tal.xsd" status="Executable">
<ExecProperties supportsParallelMostFlash="false"/>
<talLine id="tl_1" status="Executable" baseVariant="DKOMBI8" diagAddress="60">
<blFlash status="Executable">
<blFlashTA status="Executable">
<sgbmid>
<processClass>BTLD</processClass>
<id>00007732</id>
<mainVersion>2</mainVersion>
<subVersion>3</subVersion>
<patchVersion>11</patchVersion>
</sgbmid>
</blFlashTA>
<blFlashTA status="Executable">
<sgbmid>
<processClass>FLSL</processClass>
<id>00007735</id>
<mainVersion>2</mainVersion>
<subVersion>3</subVersion>
<patchVersion>11</patchVersion>
</sgbmid>
</blFlashTA>
</blFlash>
</talLine>
<executionTime actualEndTime="0" actualStartTime="0" plannedEndTime="0" plannedStartTime="0"/>
<installedECUList_Ist/>
<installedECUList_Soll/>
2.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tal xmlns="http://hello.com" schemaVersion="5.0.0" refSchemaFile="tal.xsd" status="Executable">
<ExecProperties supportsParallelMostFlash="false"/>
<talLine id="tl_3" status="Executable" baseVariant="DKOMBI8" diagAddress="60">
<cdDeploy status="Executable">
<cdDeployTA status="Executable">
<sgbmid>
<processClass>CAFD</processClass>
<id>00006d4e</id>
<mainVersion>0</mainVersion>
<subVersion>4</subVersion>
<patchVersion>11</patchVersion>
</sgbmid>
</cdDeployTA>
</cdDeploy>
</talLine>
<executionTime actualEndTime="0" actualStartTime="0" plannedEndTime="0" plannedStartTime="0"/>
<installedECUList_Ist/>
<installedECUList_Soll/>
3.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tal xmlns="http://hello.com" schemaVersion="5.0.0" refSchemaFile="tal.xsd" status="Executable">
<ExecProperties supportsParallelMostFlash="false"/>
<talLine id="tl_2" status="Executable" baseVariant="DKOMBI8" diagAddress="60">
<swDeploy status="Executable">
<swDeployTA status="Executable">
<sgbmid>
<processClass>SWFL</processClass>
<id>00007736</id>
<mainVersion>2</mainVersion>
<subVersion>3</subVersion>
<patchVersion>11</patchVersion>
</sgbmid>
</swDeployTA>
<swDeployTA status="Executable">
<sgbmid>
<processClass>SWFL</processClass>
<id>00007bfc</id>
<mainVersion>2</mainVersion>
<subVersion>3</subVersion>
<patchVersion>11</patchVersion>
</sgbmid>
</swDeployTA>
</swDeploy>
</talLine>
<executionTime actualEndTime="0" actualStartTime="0" plannedEndTime="0" plannedStartTime="0"/>
<installedECUList_Ist/>
<installedECUList_Soll/>
我试着用id删除一些特定的标签数据,但没有成功。你能不能建议我用更好的方法来实现我的目标。
import xml.etree.ElementTree as ET
tree = ET.parse('main.xml')
root = tree.getroot()
mydata = root.find(".talLine[@id='tl_1']")
mydata.remove(mydata)
提前感谢。