如何提取部分xml文件

时间:2015-04-11 21:39:06

标签: python xml elementtree

我有一个大的xml文件,看起来像下面那个。基本上我想提取部分xml文件,例如这个"<ManagedElementId string = "rbs064841"/>"

 <Model version = "1" importVersion = "12.2">
        <Create>
            <SubNetwork networkType = "WRAN" userLabel="AHPTUR14">
                <ManagedElement sourceType = "CELLO">
                    <ManagedElementId string = "rbs064841"/>
                    <primaryType type = "RBS"/>
                    <managedElementType types = ""/>
                    <associatedSite string = "Site=site06484"/>
                    <nodeVersion string = "W12B"/>
                    <platformVersion string = "Cello 12.2"/>
                    <swVersion string = ""/>
                    <vendorName string = "ERICSSON"/>
                    <userDefinedState string = ""/>
                    <managedServiceAvailability int = "1"/>
                    <isManaged boolean = "true"/>
                    <neMIMVersion string = "vS.1.150"/>
                    <connectionStatus string = "ON"/>
                </ManagedElement>
            </SubNetwork>
             <SubNetwork networkType = "WRAN" userLabel = "AHPT78">
                <ManagedElement sourceType = "CELLO">
                    <ManagedElementId string = "rbs04798"/>
                    <primaryType type = "RBS"/>
                    <managedElementType types = ""/>
                    <associatedSite string = "Site=site06484"/>
                    <nodeVersion string = "W12B"/>
                    <platformVersion string = "Cello 12.2"/>
                    <swVersion string = ""/>
                    <vendorName string = "ERICSSON"/>
                    <userDefinedState string = ""/>
                    <managedServiceAvailability int = "1"/>
                    <isManaged boolean = "true"/>
                    <neMIMVersion string = "vS.1.150"/>
                    <connectionStatus string = "ON"/>
                </ManagedElement>
            </SubNetwork>
            <SubNetwork networkType = "WRAN" userLabel = "AHPT4">
                <ManagedElement sourceType = "CELLO">
                    <ManagedElementId string = "rbs04456"/>
                    <primaryType type = "RBS"/>
                    <managedElementType types = ""/>
                    <associatedSite string = "Site=site06484"/>
                    <nodeVersion string = "W12B"/>
                    <platformVersion string = "Cello 12.2"/>
                    <swVersion string = ""/>
                    <vendorName string = "ERICSSON"/>
                    <userDefinedState string = ""/>
                    <managedServiceAvailability int = "1"/>
                    <isManaged boolean = "true"/>
                    <neMIMVersion string = "vS.1.150"/>
                    <connectionStatus string = "ON"/>
                </ManagedElement>
            </SubNetwork>
        </Create>
    </Model>

这意味着解析后我会提取这部分:

<SubNetwork networkType = "WRAN" userLabel="AHPTUR14">
            <ManagedElement sourceType = "CELLO">
                <ManagedElementId string = "rbs064841"/>
                <primaryType type = "RBS"/>
                <managedElementType types = ""/>
                <associatedSite string = "Site=site06484"/>
                <nodeVersion string = "W12B"/>
                <platformVersion string = "Cello 12.2"/>
                <swVersion string = ""/>
                <vendorName string = "ERICSSON"/>
                <userDefinedState string = ""/>
                <managedServiceAvailability int = "1"/>
                <isManaged boolean = "true"/>
                <neMIMVersion string = "vS.1.150"/>
                <connectionStatus string = "ON"/>
            </ManagedElement>
        </SubNetwork>

因此,ManagedElementId在大xml文件中进行搜索,并在找到时提取其中的部分内容,从<SubNetwork></SubNetwork>。 我知道如何从xml文件中提取数据,但我不知道如何提取xml.file的一部分。我正在使用python ElementTree。 任何建议都会有所帮助。

1 个答案:

答案 0 :(得分:1)

findpath一起使用,然后获取相对父节点,如下所示:

s = '''<Model version = "1" importVersion = "12.2">
        <Create>
            <SubNetwork networkType = "WRAN" userLabel="AHPTUR14">
                <ManagedElement sourceType = "CELLO">
                    <ManagedElementId string = "rbs064841"/>
                    <primaryType type = "RBS"/>
                    <managedElementType types = ""/>
                    <associatedSite string = "Site=site06484"/>
                    <nodeVersion string = "W12B"/>
                    <platformVersion string = "Cello 12.2"/>
                    <swVersion string = ""/>
                    <vendorName string = "ERICSSON"/>
                    <userDefinedState string = ""/>
                    <managedServiceAvailability int = "1"/>
                    <isManaged boolean = "true"/>
                    <neMIMVersion string = "vS.1.150"/>
                    <connectionStatus string = "ON"/>
                </ManagedElement>
            </SubNetwork>
             <SubNetwork networkType = "WRAN" userLabel = "AHPT78">
                <ManagedElement sourceType = "CELLO">
                    <ManagedElementId string = "rbs04798"/>
                    <primaryType type = "RBS"/>
                    <managedElementType types = ""/>
                    <associatedSite string = "Site=site06484"/>
                    <nodeVersion string = "W12B"/>
                    <platformVersion string = "Cello 12.2"/>
                    <swVersion string = ""/>
                    <vendorName string = "ERICSSON"/>
                    <userDefinedState string = ""/>
                    <managedServiceAvailability int = "1"/>
                    <isManaged boolean = "true"/>
                    <neMIMVersion string = "vS.1.150"/>
                    <connectionStatus string = "ON"/>
                </ManagedElement>
            </SubNetwork>
            <SubNetwork networkType = "WRAN" userLabel = "AHPT4">
                <ManagedElement sourceType = "CELLO">
                    <ManagedElementId string = "rbs04456"/>
                    <primaryType type = "RBS"/>
                    <managedElementType types = ""/>
                    <associatedSite string = "Site=site06484"/>
                    <nodeVersion string = "W12B"/>
                    <platformVersion string = "Cello 12.2"/>
                    <swVersion string = ""/>
                    <vendorName string = "ERICSSON"/>
                    <userDefinedState string = ""/>
                    <managedServiceAvailability int = "1"/>
                    <isManaged boolean = "true"/>
                    <neMIMVersion string = "vS.1.150"/>
                    <connectionStatus string = "ON"/>
                </ManagedElement>
            </SubNetwork>
        </Create>
    </Model>'''

# I'd prefer lxml, but you need to work on xml module...
import xml.etree.ElementTree as ET
tree = ET.fromstring(s)

# since the SubNetwork node you're interested is the parent of parent of ManagedElementId
node = tree.find('.//ManagedElementId[@string="rbs064841"]/../../../')

print ET.tostring(node)
<SubNetwork networkType="WRAN" userLabel="AHPTUR14">
                <ManagedElement sourceType="CELLO">
                    <ManagedElementId string="rbs064841"/>
                    <primaryType type="RBS"/>
                    <managedElementType types=""/>
                    <associatedSite string="Site=site06484"/>
                    <nodeVersion string="W12B"/>
                    <platformVersion string="Cello 12.2"/>
                    <swVersion string=""/>
                    <vendorName string="ERICSSON"/>
                    <userDefinedState string=""/>
                    <managedServiceAvailability int="1"/>
                    <isManaged boolean="true"/>
                    <neMIMVersion string="vS.1.150"/>
                    <connectionStatus string="ON"/>
                </ManagedElement>
            </SubNetwork>

如果要从文件解析,请使用getroot()

root = ET.parse('file.xml')
tree = root.getroot()
...

希望这有帮助。