在平时,经常会用到python解析xml文件,最近遇到python解析xml字符串,为大家分享
假设
data = '<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://XXX.org/">8888</string>'
引入
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
root = ET.fromstring(data)
balance = root.text
print(f'balance--->{balance}')
balance--->8888