基于Python的XML格式的文件示例代码详解

XML文件是可拓展标记语言,是一种简单的数据存储语言,被设计用来传输和存储数据

在Python中XML的一些方法

读取文件和内容

#引用xml模块
from xml.etree import ElementTree as ET

# ET去打开xml文件
tree = ET.parse("files/xo.xml")

# 获取根标签
root = tree.getroot()

print(root) # 
from xml.etree import ElementTree as ET

content = """

  
    2
    2023
    141100
    
    
  
   
    69
    2026
    13600
    
    
  

"""

root = ET.XML(content) # 获取根标签 
print(root) # 

读取节点数据

from xml.etree import ElementTree as ET

content = """

  
    2
    2023
    141100
    
    
  
   
    69
    2026
    13600
    
    
  

"""

# 获取根标签 data
root = ET.XML(content)

country_object = root.find("country") # 获取XML文件中的country标签
print(country_object.tag, country_object.attrib)# 获取country标签名  获取country标签地属性
gdppc_object = country_object.find("gdppc")# 获取gdppc标签
print(gdppc_object.tag,gdppc_object.attrib,gdppc_object.text)# 获取gdppc标签的名称  获取gdppc属性(没有属性为:{}) 获取gdppc标签里面的内容
from xml.etree import ElementTree as ET

content = """

  
    2
    2023
    141100
    
    
  
   
    69
    2026
    13600
    
    
  

"""

# 获取根标签 data
root = ET.XML(content)

# 获取data标签的孩子标签
for child in root:
  # child.tag = conntry 获取到两个country标签
  # child.attrib = {"name":"Liechtenstein"}
  print(child.tag, child.attrib)
  for node in child:
    print(node.tag, node.attrib, node.text) # 获取到reank标签
from xml.etree import ElementTree as ET

content = """

  
    2
    2023
    141100
    
    
  
   
    69
    2026
    13600
    
    
  

"""

root = ET.XML(content)

# 找到子子孙孙的year标签
for child in root.iter('year'):
  print(child.tag, child.text)
from xml.etree import ElementTree as ET

content = """

  
    2
    2023
    141100
    
    
  
   
    69
    2026
    13600
    
    
  

"""

root = ET.XML(content)
v1 = root.findall('country') # 找到所有的country标签
print(v1)

v2 = root.find('country').find('rank') # 找到country标签中的rank标签
print(v2.text)

删除和修改节点

from xml.etree import ElementTree as ET

content = """

  
    2
    2023
    141100
    
    
  
   
    69
    2026
    13600
    
    
  

"""

root = ET.XML(content)

# 修改节点内容和属性
rank = root.find('country').find('rank')
print(rank.text)
rank.text = "999" # 修改rank标签里面的内容
rank.set('update', '2020-11-11') # 为rank标签新增一个update属性
print(rank.text, rank.attrib)
############ 保存文件 ############
tree = ET.ElementTree(root)
tree.write("new.xml", encoding='utf-8')

# 删除节点
root.remove( root.find('country') )
print(root.findall('country'))

############ 保存文件 ############
tree = ET.ElementTree(root)
tree.write("newnew.xml", encoding='utf-8')

构建文档


  
    
    
  
  
from xml.etree import ElementTree as ET

# 创建根标签
root = ET.Element("home")

# 创建节点大儿子
son1 = ET.Element('son', {'name': '儿1'})
# 创建小儿子
son2 = ET.Element('son', {"name": '儿2'})

# 在大儿子中创建两个孙子
grandson1 = ET.Element('grandson', {'name': '儿11'})
grandson2 = ET.Element('grandson', {'name': '儿12'})
son1.append(grandson1)
son1.append(grandson2)

# 把儿子添加到根节点中
root.append(son1)
root.append(son2)

tree = ET.ElementTree(root)
tree.write('oooo.xml', encoding='utf-8', short_empty_elements=False) #short_empty_elements 是否采取短标签的形式创建

  
    
    
  
  
from xml.etree import ElementTree as ET

# 创建根节点
root = ET.Element("famliy")


# 创建大儿子
son1 = root.makeelement('son', {'name': '儿1'})
# 创建小儿子
son2 = root.makeelement('son', {"name": '儿2'})

# 在大儿子中创建两个孙子
grandson1 = son1.makeelement('grandson', {'name': '儿11'})
grandson2 = son1.makeelement('grandson', {'name': '儿12'})

son1.append(grandson1)
son1.append(grandson2)


# 把儿子添加到根节点中
root.append(son1)
root.append(son2)

tree = ET.ElementTree(root)
tree.write('oooo.xml',encoding='utf-8')

	
  	孙子
  
	
from xml.etree import ElementTree as ET


# 创建根节点
root = ET.Element("famliy")


# 创建节点大儿子
son1 = ET.SubElement(root, "son", attrib={'name': '儿1'})
# 创建小儿子
son2 = ET.SubElement(root, "son", attrib={"name": "儿2"})

# 在大儿子中创建一个孙子
grandson1 = ET.SubElement(son1, "age", attrib={'name': '儿11'})
grandson1.text = '孙子'


et = ET.ElementTree(root) #生成文档对象
et.write("test.xml", encoding="utf-8")
from xml.etree import ElementTree as ET

# 创建根节点
root = ET.Element("user")
root.text = " 
 

到此这篇关于基于Python的XML格式的文件的文章就介绍到这了,更多相关python xml格式文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(基于Python的XML格式的文件示例代码详解)