我正在尝试解析包含这些标记的XML文件。
<?xml version="4.0" encoding="utf-8"?>
<phrases>
<phrase title="bacd_dd" version_id="10" version_string="lphaf"><![CDATA[bacd dsfbsd dfsd]]></phrase>
<phrase title="bcvd_ff" version_id="10" version_string="lphaf"><![CDATA[ans fkdfjid dfdf]]></phrase>
<phrase title="bdsd_fffd" version_id="17" version_string="lphaf 7"><![CDATA[jdhfd dsfodf wernksdlg ffguywer
<BR>
dsf
sddsfdsfdsf ksdfj fdsf]]></phrase>
</phrases>
现在我想只获取标签值。我如何解析整个XML文件?
答案 0 :(得分:2)
使用xml.etree
import xml.etree.ElementTree as ET
root = ET.fromstring("""<?xml version="1.0" encoding="utf-8"?>
<phrases>
<phrase title="bacd_dd" version_id="1010010" version_string="1.1.0 Alpha"><![CDATA[bacd dsfbsd dfsd]]></phrase>
<phrase title="bcvd_ff" version_id="1010010" version_string="1.1.0 Alpha"><![CDATA[ans fkdfjid dfdf]]></phrase>
<phrase title="bdsd_fffd" version_id="1000017" version_string="1.0.0 Alpha 7"><![CDATA[jdhfd dsfodf wernksdlg ffguywer
<BR>
dsf
sddsfdsfdsf ksdfj fdsf]]></phrase>
</phrases>""")
print root.tag
>>>'phrases'
for i in root:
print i.text
>>>bacd dsfbsd dfsd
ans fkdfjid dfdf
jdhfd dsfodf wernksdlg ffguywer
<BR>
dsf
sddsfdsfdsf ksdfj fdsf
for i in root:
print i.attrib
>>>{'version_string': '1.1.0 Alpha', 'version_id': '1010010', 'title': 'bacd_dd'}
{'version_string': '1.1.0 Alpha', 'version_id': '1010010', 'title': 'bcvd_ff'}
{'version_string': '1.0.0 Alpha 7', 'version_id': '1000017', 'title': 'bdsd_fffd'}
如果需要从xml
文件进行解析。
import xml.etree.ElementTree as ET
tree = ET.parse('file.xml')
root = tree.getroot()
有关详情,请参阅https://docs.python.org/2/library/xml.etree.elementtree.html