Python XML概述说明

活动地址:CSDN21天学习挑战赛
目录
一、什么是XML?1、简介:2、XML作用3、XML参数解读4、ElementTree1)ElementTree对象方法2)属性方法3)对象4)模块方法5)实战例子:6)DOM方式
5、Python操作XML文件1)创建xml文件2)插入xml文件
6、Python操作json和xml互相转换
一、什么是XML?
1、简介:
XML被设计师用来传输和存储数据。XML指可被扩展标记语言(Extensible Markup Language),是一种标记语言,是从标准通用标记语言(SGML)中简化修改出来的。它主要用到的有可扩展标记语言、可扩展样式语言(XSL)、XBRL和XPath等。
2、XML作用
XML在Web中起到的作用不会亚于一直作为Web基石的HTML。XML是各种应用程序之间进行数据传输最常用的工具,而且不受平台限制。
3、XML参数解读
4、ElementTree
ElementTree 模块提供了一个轻量级、Pythonic的API,同时还有一个搞笑的C语言实现,即XML.etree.cElementTree.与DOM相比较,ElementTree的速度更快,API使用更直接、方便。与SAX相比较,ElementTree.iterparse函数同样提供了按需解析的功能,不会一次性在内存中读入整个文档,ElementTree的性能与SAX模块大致厢房,但是它的API更加高层次,用户使用起来更加便捷。
1)ElementTree对象方法
类方法作用Element.getchildren()Element.getiterator(tag=None)Element.iter(tag=None)遍历该Element所有后代,也可以指定tag进行遍历寻找。Element.iterfind(path, namespaces=None)根据tag或path查找所有的后代。Element.itertext()遍历所有后代并返回text值。Element.findall(path)查找当前元素下tag或path能够匹配的直系节点。Element.findtext(path, default=None, namespaces=None)寻找第一个匹配子元素,返回其text值。匹配对象可以为tag或path。Element.find(path)查找当前元素下tag或path能够匹配的首个直系节点。Element.text获取当前元素的text值。Element.get(key, default=None)获取元素指定key对应的属性值,如果没有该属性,则返回default值。Element.keys()返回元素属性名称列表Element.items()返回(name,value)列表Element.getiterator(self, tag=None)
注意:Element.findall()或者Element.find()方法,只会从结点的直接子结点中查找,并不会递归查找。
2)属性方法
方法名说明Element.tag节点名(tag)(str)(dict) Element.attrib属性(attributes)Element.text文本(text)(str)(str) Element.tail附加文本(tail)Element[:]子节点列表(list)
3)对象
类名方法find(match)findall(match)findtext(match, default=None)getroot()获取根节点iter(tag=None)iterfind(match)parse(source, parser=None)装载xml对象,source可以为文件名或文件类型对象.write(file, encoding=“us-ascii”, xml_declaration=None, default_namespace=None,method=“xml”)
4)模块方法
函数方法Valuexml.etree.ElementTree.canonicalize(xml_data=None, *, out=None, from_file=None, **options)xml.etree.ElementTree.Comment(text=None)创建一个特别的element,通过标准序列化使其代表了一个comment。comment可以为bytestring或unicode。xml.etree.ElementTree.dump(elem)生成一个element tree,通过sys.stdout输出,elem可以是元素树或单个元素。这个方法最好只用于debug。xml.etree.ElementTree.fromstring(text, parser=None)text是一个包含XML数据的字符串,与XML()方法类似,返回一个Element实例。xml.etree.ElementTree.fromstringlist(sequence, parser=None) 从字符串的序列对象中解析xml文档。缺省parser为XMLParser,返回Element实例。xml.etree.ElementTree.indent(tree, space=’ ', level=0)xml.etree.ElementTree.iselement(element)检查是否是一个element对象。xml.etree.ElementTree.iterparse(source, events=None, parser=None)将文件或包含xml数据的文件对象递增解析为element tree,并且报告进度。events是一个汇报列表,如果忽略,将只有end事件会汇报出来。xml.etree.ElementTree.parse(source, parser=None)将一个文件或者字符串解析为element tree。xml.etree.ElementTree.ProcessingInstruction(target, text=None)这个方法会创建一个特别的element,该element被序列化为一个xml处理命令。xml.etree.ElementTree.register_namespace(prefix, uri)注册命名空间前缀。这个注册是全局有效,任何已经给出的前缀或者命名空间uri的映射关系会被删除。xml.etree.ElementTree.SubElement(parent, tag, attrib={}, **extra)子元素工厂,创建一个Element实例并追加到已知的节点。xml.etree.ElementTree.tostring(element, encoding=‘us-ascii’, method=‘xml’, *, xml_declaration=None, default_namespace=None, short_empty_elements=True)生成一个字符串来表示表示xml的element,包括所有子元素。element是Element实例,method为"xml",“html”,“text”。返回包含了xml数据的字符串。xml.etree.ElementTree.tostringlist(element, encoding=‘us-ascii’, method=‘xml’, *, xml_declaration=None, default_namespace=None, short_empty_elements=True)生成一个字符串来表示表示xml的element,包括所有子元素。element是Element实例,method为"xml",“html”,“text”。返回包含了xml数据的字符串列表。xml.etree.ElementTree.XML(text, parser=None)从一个字符串常量中解析出xml片段。返回Element实例。xml.etree.ElementTree.XMLID(text, parser=None)从字符串常量解析出xml片段,同时返回一个字典,用以映射element的id到其自身。
5)实战例子:
-创建xml文件
编写完整代码和注释
import xml.etree.ElementTree as ET
tree = ET.ElementTree(file='test.xml') #加载文档
print(type(tree))
root = tree.getroot() #获取根元素
print(type(root))
print(root.tag,root.attrib) #查看属性
for index,elem in enumerate(root): #遍历子元素属性
print("第%s个%s元素,属性: %s " % (index,elem.tag,elem.attrib))
for i,elem_name in enumerate(elem): #遍历子元素内容
print("标签: %s, 内容: %s" % (elem_name.tag,elem_name.text))
打印结果
dependencies {'self': 'are you ok?'}
第0个dependency元素,属性: {'name': 'app1'}
标签: groupId, 内容: com.alibaba
标签: artifactId, 内容: fastjson
标签: version, 内容: 1.2.68
第1个dependency元素,属性: {'name': 'app2'}
标签: groupId, 内容: org.apache.flink
标签: artifactId, 内容: flink-csv
标签: version, 内容: 1.12.4
第2个dependency元素,属性: {'name': 'app3'}
标签: groupId, 内容: com.google.code.gson
标签: artifactId, 内容: gson
标签: version, 内容: 2.8.5
代码图
6)DOM方式
DOM(Document Object Model)将XML文档座位一棵树状结构进行分析,获取节点的内容以及相关属性,或是新增、删除和修改节点的内容。XML解析器在加载XML文件以后,DQM模式将XML文件的元素视为一个树状结构的节点,一次性读入内存。
编写完整代码
from xml.dom.minidom import parse
dom = parse('test.xml') # 读取文件
elem = dom.documentElement # 获取文档元素对象
dependency_elem = elem.getElementsByTagName('dependency') #获取dependency
print(dependency_elem)
print(type(dependency_elem))
for dep_elem in dependency_elem:
alibaba = dep_elem.getElementsByTagName('groupId')[0].childNodes[0].nodeValue #获取标签内容
flink = dep_elem.getElementsByTagName('artifactId')[0].childNodes[0].nodeValue
version = dep_elem.getElementsByTagName('version')[0].childNodes[0].nodeValue
print('alibaba: ',alibaba,'flink: ',flink,'version: ',version)
打印结果
[
alibaba: com.alibaba flink: fastjson version: 1.2.68
alibaba: org.apache.flink flink: flink-csv version: 1.12.4
alibaba: com.google.code.gson flink: gson version: 2.8.5
代码图
5、Python操作XML文件
语法:writexml(file,indent=‘’,addindent=‘’,newl=‘’,encoding=None) 参数说明:
file:保存的文件对象indent:根节点缩进间隔allindent:子节点缩进间隔newl:新行,指明换行方式encoding:保存文件编码格式
doc.writexml() 生成xml文档,将在内存的xml文档写入到硬盘中才能看到新创建的xml文档
1)创建xml文件
import xml.dom.minidom
# 在内存中创建一个xml文档
doc = xml.dom.minidom.Document()
# 创建根元素
root = doc.createElement('dependencies')
# 设置根元素的属性
root.setAttribute('type', 'are you ok?')
# 将根节点添加到文档对象中
doc.appendChild(root)
# 创建子元素
depend = doc.createElement('dependency')
# 添加注释
depend.appendChild(doc.createComment('helloworld'))
# 设置子元素属性
depend.setAttribute('name', 'app1')
# 嵌套子元素,添加文本节点
groupId = doc.createElement('groupId')
groupId.appendChild(doc.createTextNode('com.alibaba'))
artifactId = doc.createElement('artifactId')
artifactId.appendChild(doc.createTextNode('fastjson'))
version = doc.createElement('version')
version.appendChild(doc.createTextNode('1.2.68'))
# 将子元素添加到dependency中
depend.appendChild(groupId)
depend.appendChild(artifactId)
depend.appendChild(version)
# 将dependency添加到root根元素
root.appendChild(depend)
# 创建子元素
depend = doc.createElement('dependency')
# 设置子元素属性
depend.setAttribute('name', 'app2')
# 嵌套子元素,添加文本节点
groupId = doc.createElement('groupId')
groupId.appendChild(doc.createTextNode('org.apache.flink'))
artifactId = doc.createElement('artifactId')
artifactId.appendChild(doc.createTextNode('flink-csv'))
version = doc.createElement('version')
version.appendChild(doc.createTextNode('1.12.4'))
# 将子元素添加到dependency中
depend.appendChild(groupId)
depend.appendChild(artifactId)
depend.appendChild(version)
# 将dependency添加到root根元素
root.appendChild(depend)
print(root.toxml())
# 需要指定格式,不然在notepad++显示乱码
fp = open('pom.xml', 'w', encoding='utf-8')
doc.writexml(fp, indent='', addindent='\t', newl='\n', encoding='utf-8')
# 用完要记得关闭
fp.close()
结果显示:
2)插入xml文件
import xml.dom.minidom
# 在内存中创建一个xml文档
doc = xml.dom.minidom.Document()
# 创建根元素
root = doc.createElement('dependencies')
# 设置根元素的属性
root.setAttribute('type', 'are you ok?')
# 将根节点添加到文档对象中
doc.appendChild(root)
# 创建子元素
depend = doc.createElement('dependency')
# 添加注释
depend.appendChild(doc.createComment('helloworld'))
# 设置子元素属性
depend.setAttribute('name', 'app1')
# 嵌套子元素,添加文本节点
groupId = doc.createElement('groupId')
groupId.appendChild(doc.createTextNode('com.alibaba'))
artifactId = doc.createElement('artifactId')
artifactId.appendChild(doc.createTextNode('fastjson'))
version = doc.createElement('version')
version.appendChild(doc.createTextNode('1.2.68'))
# 将子元素添加到dependency中
depend.appendChild(groupId)
depend.appendChild(artifactId)
depend.appendChild(version)
# 将dependency添加到root根元素
root.appendChild(depend)
# 创建子元素
depend = doc.createElement('dependency')
# 设置子元素属性
depend.setAttribute('name', 'app2')
# 嵌套子元素,添加文本节点
groupId = doc.createElement('groupId')
groupId.appendChild(doc.createTextNode('org.apache.flink'))
artifactId = doc.createElement('artifactId')
artifactId.appendChild(doc.createTextNode('flink-csv'))
version = doc.createElement('version')
version.appendChild(doc.createTextNode('1.12.4'))
# 将子元素添加到dependency中
depend.appendChild(groupId)
depend.appendChild(artifactId)
depend.appendChild(version)
# 将dependency添加到root根元素
root.appendChild(depend)
# 创建子元素
depend = doc.createElement('dependency')
# 设置子元素属性
depend.setAttribute('name', 'app3') # 嵌套子元素,添加文本节点
groupId = doc.createElement('groupId')
groupId.appendChild(doc.createTextNode('com.google.code.gson'))
artifactId = doc.createElement('artifactId')
artifactId.appendChild(doc.createTextNode('gson'))
version = doc.createElement('version')
version.appendChild(doc.createTextNode('2.8.5'))
# 将子元素添加到dependency中
depend.appendChild(groupId)
depend.appendChild(artifactId)
depend.appendChild(version)
# 将dependency添加到root根元素
root.appendChild(depend)
print(root.toxml())
# 需要指定格式,不然在notepad++显示乱码
fp = open('pom.xml', 'w', encoding='utf-8')
doc.writexml(fp, indent='', addindent='\t', newl='\n', encoding='utf-8')
# 用完要记得关闭
fp.close()
插入结果:
6、Python操作json和xml互相转换
json和xml互相转换https://blog.csdn.net/walykyy/article/details/126159105?spm=1001.2014.3001.5501