'''
"D:\three_test\gpn_InternetGatewayDevice_v2.xml"
<SOAP-ENV:Envelope>
<SOAP-ENV:Header>
<cwmp:ID SOAP-ENV:mustUnderstand="1">1</cwmp:ID>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<cwmp:GetParameterNamesResponse>
<ParameterList xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="cwmp:ParameterInfoStruct[6570]">
<ParameterInfoStruct>
<Name>InternetGatewayDevice.WANDeviceNumberOfEntries</Name>
<Writable>0</Writable>
</ParameterInfoStruct>
<ParameterInfoStruct>
<Name>InternetGatewayDevice.WANDevice.</Name>
<Writable>0</Writable>
</ParameterInfoStruct>
<ParameterInfoStruct>
</cwmp:GetParameterNamesResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
'''
import xmltodict
#将xml中的数据读入内存并整个转换成一个字典,放入doc
with open(r"D:\three_test\gpn_InternetGatewayDevice_v2.xml") as fd:
doc = xmltodict.parse(fd.read())
#取字典中的数据也很简单,按照xml文件中的格式依次往下即可
#e.g:取结点属性
print(doc["SOAP-ENV:Envelope"]["SOAP-ENV:Header"]["cwmp:ID"]['@SOAP-ENV:mustUnderstand']) #1
print(doc['SOAP-ENV:Envelope']['SOAP-ENV:Body']['cwmp:GetParameterNamesResponse']['ParameterList']['@xsi:type']) #SOAP-ENC:Array
#e.g.取结点值
print(doc["SOAP-ENV:Envelope"]["SOAP-ENV:Header"]["cwmp:ID"]['#text']) #1
print(doc['SOAP-ENV:Envelope']['SOAP-ENV:Body']['cwmp:GetParameterNamesResponse']['ParameterList']['ParameterInfoStruct'][0]['Name']) #InternetGatewayDevice.WANDeviceNumberOfEntries
#e.g.如果是multiobject,将返回一个列表,列表中每一个元素都是一个对象OrderedDict([('Name', 'XXX'), ('Writable', 'X')])
list = doc["SOAP-ENV:Envelope"]["SOAP-ENV:Body"]["cwmp:GetParameterNamesResponse"]["ParameterList"]["ParameterInfoStruct"]
#遍历列表中的每一个子字典,i为列表下标
for i,obj in enumerate(list):
print(obj['Name'])
print(obj['Writable'])
break;
#加入下面这两条指令,代码运行到此处就相当于打了一个断点,这中方式确定下路径是否正确效率更高。
import pdb
pdb.set_trace()