[python3]使用ElementTree处理xml

# cat 1.xml

    
    c++ primer
    

#!/bin/env python3
#file: python.xml.py

from io import StringIO as StringIO
from xml.etree import ElementTree
o=open("1.xml")
f=StringIO(o.read())
o.close()
tree=ElementTree.parse(f)
f.close()

tree.getroot()
tree.iter('book')

for elmt in tree.iter('book'):
        print(elmt.tag)
        print(elmt.text)
# python3 python.xml.py
book

    c++ primer

你可能感兴趣的:(开发语言,python,xml)