[我有一个大的xml文件,在这里我想根据id将xml文件分成多个文件(这里的id是唯一的)。目前有一个带有3个唯一标记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/>
</tal>
我需要在具有页眉和页脚的单个文件中找到每个“ 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/>
</tal>
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/>
</tal>
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/>
</tal>
我试图删除一些带有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)
提前感谢。
我们只需要从根目录中找到标签的索引并清除它们,然后保存到新的xml文件中
import xml.etree.ElementTree as ET
mytree = ET.parse('D://talfiles//TAL_High_Hud_Dcs_002_003_011.xml')