xml 查找总是 None

xml find is always None

如何使用 .find.get 来获得我想要的值?

我看例子,不知道哪里做错了

总是得到None

<?xml version="1.0" encoding="UTF-8"?>
<config version="1.0" xmlns="http://www.ipc.com/ver10">
    <types>
        <bitRateType>
            <enum>VBR</enum>
            <enum>CBR</enum>
        </bitRateType>
        <quality>
            <enum>lowest</enum>
            <enum>lower</enum>
        </quality>
    </types>
    <streams type="list" count="2">
        <item id="0">
            <name type="string" maxLen="32"><![CDATA[rtsp://192.168.0.175:554/chID=2&streamType=main]]></name>
            <resolution>2592x1520</resolution>
        </item>
        <item id="1">
            <name type="string" maxLen="32"><![CDATA[rtsp://192.168.0.175:554/chID=2&streamType=sub1]]></name>
            <resolution>704x480</resolution>
        </item>
    </streams>
</config>

我的代码。

tree = ET.fromstring(res.text)
types = tree.find('types')
streams = tree.find('streams')
item = streams.findall('item')[0]
print(item.get('name'))
print(item.get('resolution'))
print(types, streams, item)

你可以试试这个方法的

from xml.etree import cElementTree as ET
new_tree = ET.parse('test.xml')
new_root = new_tree.getroot()
types = new_root[0]
bitRateType = types[0]
quality = types[1]
streams = new_root[1]
item0_name = streams[0][0]
item0_resolution = streams[0][1]
item1_name = streams[1][0]
item1_resolution = streams[1][1]
print(item0_name.attrib)
print(item1_resolution.text)

var.attrib - 所有属性,例如:

print(item0_name.attrib)
{'type': 'string', 'maxLen': '32'}

var.text - 获取文本,例如:

print(item1_resolution.text)
704x480