在给定的标签下将XML文件分割成多个,例如:使用python的标签

0 人关注

我有一个大的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)

提前感谢。

4 个评论
我看不到你的代码显示你尝试了什么,或者关于你的代码的问题,说什么没有工作。
Hi @ChrisDoyle im very new to python, even I dont how to proceed.
那么Stack overflow就不是你要找的网站了。这不是一个教程,也不是一个期望有人为你写代码的网站,外面有很多网站会教你如何用python解析xml。
嗨,@ChrisDoyle,我已经尝试了一些东西,请你检查一次。
python
python-3.x
xml
xml-parsing
Madhu
Madhu
发布于 2019-12-26
1 个回答
Madhu
Madhu
发布于 2019-12-31
已采纳
0 人赞同

我们只需要从根中找出你的标签的索引,清除它们并保存到新的xml文件中。

import xml.etree.ElementTree as ET
mytree=ET.parse('D://talfiles//TAL_High_Hud_Dcs_002_003_011.xml')
myroot=mytree.getroot()
myroot[1].clear()
myroot[2].clear()
mytree.write('D://talfiles//1.xml')

然后你的输出将被保存到新的文件中

1.xml file

<ns0:tal xmlns:ns0="http:hello.com" refSchemaFile="tal.xsd" schemaVersion="5.0.0" status="Executable">
<ns0:ExecProperties supportsParallelMostFlash="false" />
<ns0:talLine /><ns0:talLine /><ns0:talLine baseVariant="DKOMBI8" diagAddress="60" id="tl_3" status="Executable">
    <ns0:cdDeploy status="Executable">
        <ns0:cdDeployTA status="Executable">
            <ns0:sgbmid>
                <ns0:processClass>CAFD</ns0:processClass>
                <ns0:id>00006d4e</ns0:id>
                <ns0:mainVersion>0</ns0:mainVersion>
                <ns0:subVersion>4</ns0:subVersion>
                <ns0:patchVersion>11</ns0:patchVersion>
            </ns0:sgbmid>
        </ns0:cdDeployTA>
    </ns0:cdDeploy>
</ns0:talLine>
<ns0:executionTime actualEndTime="0" actualStartTime="0" plannedEndTime="0" plannedStartTime="0" />
<ns0:installedECUList_Ist />
<ns0:installedECUList_Soll />