简体   繁体   中英

xml find is always None

How can I use .find or .get to get the value what's I want?

I look at the example, I don't know where I did it wrong

Always 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>

My Code.

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)

You can try this method's

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 - all attributes, for example:

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

var .text - get text , for example:

print(item1_resolution.text)
704x480

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM