在xml.etree.ElementTree的帮助下,使用命名空间从XML检索数据

时间:2018-04-06 19:29:04

标签: python xml xml-parsing xml-namespaces

我有以下要解析的xml,并在词典 包含 列表中获取' SetOfFiles ' strong>格式然而,尽管尝试了许多排列和组合,我无法将这些数据放入字典列表中。

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <ns1:SelectLogFilesResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
            xmlns:ns1="http://schemas.cisco.com/ast/soap/">
            <FileSelectionResult xsi:type="ns2:SchemaFileSelectionResult"
                xmlns:ns2="http://cisco.com/ccm/serviceability/soap/LogCollection/">
                <Node xsi:type="ns2:Node">
                    <name xsi:type="xsd:string">10.201.196.84</name>
                    <ServiceList soapenc:arrayType="ns2:ServiceLogs[1]" xsi:type="soapenc:Array"
                        xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
                        <item xsi:type="ns2:ServiceLogs">
                            <name xsi:type="xsd:string" xsi:nil="true"/>
                            <SetOfFiles soapenc:arrayType="ns2:file[2]" xsi:type="soapenc:Array">
                                <item xsi:type="ns2:file">
                                    <name xsi:type="xsd:string">SDL002_200_000179.txt.gzo</name>
                                    <absolutepath xsi:type="xsd:string">/var/log/active/cm/trace/cti/sdl/SDL002_200_000179.txt.gzo</absolutepath>
                                    <filesize xsi:type="xsd:string">262967</filesize>
                                    <modifiedDate xsi:type="xsd:string">Thu Apr 05 13:02:57 CDT 2018</modifiedDate>
                                </item>
                                <item xsi:type="ns2:file">
                                    <name xsi:type="xsd:string">SDL002_100_000986.txt.gzo</name>
                                    <absolutepath xsi:type="xsd:string">/var/log/active/cm/trace/ccm/sdl/SDL002_100_000986.txt.gzo</absolutepath>
                                    <filesize xsi:type="xsd:string">912868</filesize>
                                    <modifiedDate xsi:type="xsd:string">Thu Apr 05 13:02:56 CDT 2018</modifiedDate>
                                </item>
                            </SetOfFiles>
                        </item>
                    </ServiceList>
                </Node>
            </FileSelectionResult>
            <ScheduleList soapenc:arrayType="ns3:Schedule[0]" xsi:type="soapenc:Array"
                xmlns:ns3="http://cisco.com/ccm/serviceability/soap/LogCollection/"
                xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"/>
            </ns1:SelectLogFilesResponse>
        </soapenv:Body>
    </soapenv:Envelope>

到目前为止我所尝试的是以下内容并没有给出任何输出:

reffering: https://docs.python.org/3.6/library/xml.etree.elementtree.html

import xml.etree.ElementTree as ET
root = ET.fromstring(log)
ns={'ns1': 'http://schemas.cisco.com/ast/soap/', 'soapenv': 'http://schemas.xmlsoap.org/soap/envelope/'}
root.findall('soapenv:Envelope/soapenv:Body/ns1:SelectLogFilesResponse/FileSelectionResult/Node/ServiceList/item/SetOfFiles/item',ns)

获取文本的一种方法是执行以下操作,但不提供相关数据

for i in root.iter('absolutepath ')
    print(i.text)  

1 个答案:

答案 0 :(得分:0)

哦,是的,我可以做以下事情:

`for i in range(0,len(root[0][0][0][0][1][0][1])):
    l=[]
    for f in root[0][0][0][0][1][0][1][i]:
        l.append(f.text)
    d['file'+str(i+1)]=l`

输出:

`{'file1': ['SDL002_200_000179.txt.gzo', '/var/log/active/cm/trace/cti/sdl/SDL002_200_000179.txt.gzo', '262967', 'Thu Apr 05 13:02:57 CDT 2018'], 'file2': ['SDL002_100_000986.txt.gzo', '/var/log/active/cm/trace/ccm/sdl/SDL002_100_000986.txt.gzo', '912868', 'Thu Apr 05 13:02:56 CDT 2018']}`

谢谢你。你为我做的很简单。