<breakfast_menu>
<food>
<name>Belgian Wafflesname>
<price>$5.95price>
<description>
Two of our famous Belgian Waffles with plenty of real maple syrup
description>
<calories>650calories>
food>
<food>
<name>Strawberry Belgian Wafflesname>
<price>$7.95price>
<description>
Light Belgian waffles covered with strawberries and whipped cream
description>
<calories>900calories>
food>
breakfast_menu>
import xml.etree.ElementTree as ET
xml_string ='''
Belgian Waffles
$5.95
Two of our famous Belgian Waffles with plenty of real maple syrup
650
Strawberry Belgian Waffles
$7.95
Light Belgian waffles covered with strawberries and whipped cream
900
Berry-Berry Belgian Waffles
$8.95
Light Belgian waffles covered with an assortment of fresh berries and whipped cream
900
French Toast
$4.50
Thick slices made from our homemade sourdough bread
600
Homestyle Breakfast
$6.95
Two eggs, bacon or sausage, toast, and our ever-popular hash browns
950
'''
root = ET.fromstring(xml_string)
print(root) ==> <Element 'breakfast_menu' at 0x101f94a98>
(2)从xml文件中读取,用getroot获取根节点,根节点也是Element对象
import xml.etree.ElementTree as ET
tree = ET.parse('xml_test')
root = tree.getroot()
print(tree) ==> <xml.etree.ElementTree.ElementTree object at 0x104100a20>
print(root) ==> <Element 'breakfast_menu' at 0x101d94a98>这个通第一种方式直接获取的结果一样
tag = element.tag
attrib = element.attrib # 字典
value = element.text
(2)访问子节点元素对象及其标签、属性、值
# 这里的for i in root只能访问root的直接子元素,下面的for I in root.iter()是访问直接子元素。
for child in root:
print(child,child.tag,child.attrib,child.text)
for child_child in child:
print(child_child, child_child.tag, child_child.attrib, child_child.text)
#结果类似如下:
<Element 'food' at 0x101e94bd8> food {}
<Element 'name' at 0x104111cc8> name {} Belgian Waffles<Element 'price' at 0x104570278> price {} $5.95
<Element 'description' at 0x1045702c8> description {}
Two of our famous Belgian Waffles with plenty of real maple syrup
<Element 'calories' at 0x104570908> calories {} 650
<Element 'food' at 0x10457a9f8> food {}
<Element 'name' at 0x10457aa48> name {} Strawberry Belgian Waffles
<Element 'description' at 0x10457ab38> description {}
Light Belgian waffles covered with strawberries and whipped cream
<Element 'calories' at 0x10457ab88> calories {} 900
(3)Elements元素对象都是可迭代的对象,可以直接对其list(Element)将其转化为列表或者直接索引取:
import xml.etree.ElementTree as ET
tree = ET.parse('xml_test')
root = tree.getroot()
print(list(root)) ==>[<Element 'food' at 0x101c94bd8>, <Element 'food' at 0x10457aa48>, <Element 'food' at 0x10457ac28>, <Element 'food' at 0x10457ae08>, <Element 'food' at 0x10457af98>]
print(root[0],root[1])
如上,list(root)的结果就是其3个子元素组成的列表,这时可以访问其标签、属性、值,然后对其每个子元素也可以同样的方法转换为列表访问各个属性,当然可以通过迭代的方法用for循环来操作。
(4)按照元素名字访问或者迭代元素
1. Element.iter("tag"),可以罗列该节点所包含的所有其他节点(element对象)
print(root.iter()) :返回一个可迭代对象,迭代这个对象可以迭代出包括根节点在内的所有元素节点
print(list(root.iter())) :返回一个列表,将所有元素对象放在一个列表中
print(root.iter('name')) :返回一个可迭代对象,迭代这个对象可以迭代出所有元素标签名为name的元素elemen对象
print(list(root.iter('name'))):返回一个列表,将所有标签名为name的元素对象放到一个列表中
2. Element.findall("tag"):查找当前元素为“tag”的直接子元素,tag不能省略
3. Element.find("tag"):查找为tag的第一个直接子元素,如没有,返回None
(5)修改XML文件
ElementTree.write("xml_test"):更新xml文件
Element.append(element):为当前element对象添加子元素(element)
Element.set(key,value):为当前element的key属性设置value值
Element.remove(element):删除为element的节点
#读取待修改xml文件
updateTree = ET.parse("xml_test")
root = updateTree.getroot()
#创建新节点并添加为root的子节点
newEle = ET.Element("NewElement")
newEle.attrib = {"name":"NewElement","age":"20"}
newEle.text = "This is a new element"
root.append(newEle)
#修改sub1的name属性
sub1 = root.find("food")
sub1.set("name","New Name")
#修改sub2的数据值
sub2 = root.find("sub2")
sub2.text = "New Value"
#写回原文件
updateTree.write("xml_test")
# sample.xml
<data data_attrib="hello xml" data_attrib2="hello xml2">
<country name="Liechtenstein">
<rank>1rank>
<year>2008year>
<gdppc>141100gdppc>
<neighbor direction="E" name="Austria">textqqqneighbor>
<neighbor direction="W" name="Switzerland" />
country>
<country name="Singapore">
<rank>4rank>
<year>2011year>
<gdppc>59900gdppc>
<neighbor direction="N" name="Malaysia" />
country>
<country name="Panama">
<rank>68rank>
<year>2012year>
<gdppc>13600gdppc>
<neighbor direction="W" name="Costa Rica" />
<neighbor direction="E" name="Colombia" />
country>
<country name="China">
<rank>8rank>
<neighbor direction="E" name="Japan">I am Japanneighbor>
country>
data>
tree = ElementTree.parse('sample.xml')
root = tree.getroot()
for country in root.findall('country'):
if country.attrib["name"] == "China":
neighbor = country.find("neighbor")
neighbor.text = "I am Japan"
tree.write('sample.xml')