xml文件:country.xml
<data>
<country name="shdi2hajk">231
<rank>1<NewNode A="1">This is NEW</NewNode></rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor direction="E" name="Austria" />
<neighbor direction="W" name="Switzerland" />
</country>
<country name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor direction="N" name="Malaysia" />
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor direction="W" name="Costa Rica" />
<neighbor direction="E" name="Colombia" />
</country>
<MediaPlatformService height="165" ip="36.32.160.199" passWord="111" port="9084" userName="admin" width="220">
</MediaPlatformService>
</data>
xml文件解读
1.xml一个节点有三个属性:tag、text、attrib
2. 以第一个子节点country为例:
3. tag代表节点名字,country节点的tag就是它的名字:country
4. text代表节点文本内容,rank节点的text就是1
5. attrib代表节点包含的属性,以{属性:值}这样的字典形式存放。country节点的属性是{name:Liechtenstein}.name是属性的键,Liechtenstein是属性的值。{属性:值}就是一个字典类型,可以使用一切字典方法。
6. country节点的tag为country,attrib为{name:Liechtenstein},text为空
7. rank节点的tag为rank,attrib为空字典,text为1
8. 综上所述,xml文档主要由节点以及节点的三个属性组成。
读取文件:
import xml.etree.ElementTree as ET
file_path = r'xml_te.xml'
tree = ET.ElementTree(file = file_path)
print(tree.iter())
for i in tree.iter('rank'):
print(i.text)
nodes = tree.find('country')
print(nodes.tag)
nodes2 = tree.findall('country')
print(nodes2)
for node in nodes2:
print(node.tag)
print(node.attrib)
print(node.text)
增加新节点及修改属性值和文本
import xml.etree.ElementTree as ET
file_path = r'xml_te.xml'
tree = ET.ElementTree(file = file_path)
"""增加新节点"""
net = ET.Element('NewNode')
net.attrib = {'A':"1"}
net.text = "This is NEW"
node = tree.find('country/rank/NewNode')
node.append(net)
print(node.text)
tree.write(file_path)
"""修改属性值"""
sub = tree.find('country')
sub.set('name',"shdi2hajk")
sub.text = '231'
print(sub.attrib)
print(sub.text)
tree.write(file_path)
XPATH路径: https://www.w3school.com.cn/xpath/xpath_syntax.asp
|