Python XML概述说明

本文涉及的产品
实时计算 Flink 版,5000CU*H 3个月
简介: Python XML概述说明

一、什么是XML?

1、简介:

XML被设计师用来传输和存储数据。

XML指可被扩展标记语言(Extensible Markup Language),是一种标记语言,是从标准通用标记语言(SGML)中简化修改出来的。它主要用到的有可扩展标记语言、可扩展样式语言(XSL)、XBRL和XPath等。

2、XML作用

XML在Web中起到的作用不会亚于一直作为Web基石的HTML。

XML是各种应用程序之间进行数据传输最常用的工具,而且不受平台限制。

3、XML参数解读

<?xml version="1.0" encoding="UTF-8"?>   <!--声明 version版本多少,编码样式 -->
<note>  <!--根目录  root-->
    <to>Tove</to>  <!--4个子元素 to、from、heading、body-->
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note> <!--根元素结尾,结束标志-->

4、ElementTree

ElementTree 模块提供了一个轻量级、Pythonic的API,同时还有一个搞笑的C语言实现,即XML.etree.cElementTree.与DOM相比较,ElementTree的速度更快,API使用更直接、方便。与SAX相比较,ElementTree.iterparse函数同样提供了按需解析的功能,不会一次性在内存中读入整个文档,ElementTree的性能与SAX模块大致厢房,但是它的API更加高层次,用户使用起来更加便捷。


1)ElementTree对象方法

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)模块方法

函数方法 Value

xml.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文件

<?xml version="1.0" encoding="UTF-8"?>
<dependencies self="are you ok?">
    <dependency name="app1">
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.68</version>
    </dependency>
    <dependency name="app2">
        <groupId>org.apache.flink</groupId>
        <artifactId>flink-csv</artifactId>
        <version>1.12.4</version>
    </dependency>
    <dependency name="app3">
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.5</version>
    </dependency>
</dependencies>


编写完整代码和注释

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))


打印结果

<class 'xml.etree.ElementTree.ElementTree'>
<class 'xml.etree.ElementTree.Element'>
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


代码图

d7d464c04d244baaddea66e26383efd.png

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)


打印结果

[<DOM Element: dependency at 0x16024d0>, <DOM Element: dependency at 0x161d1b0>, <DOM Element: dependency at 0x161d3f0>]
<class 'xml.dom.minicompat.NodeList'>
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

代码图

490f1ed83ea8e175881a63e57110399.png

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()


结果显示:

df3be986cc9b433365378bf35001518.png


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()

插入结果:

5cf70c98cb1c792d0325a7fa9f6cc60.png


6、Python操作json和xml互相转换

json和xml互相转换https://blog.csdn.net/walykyy/article/details/126159105?spm=1001.2014.3001.5501


相关实践学习
基于Hologres轻松玩转一站式实时仓库
本场景介绍如何利用阿里云MaxCompute、实时计算Flink和交互式分析服务Hologres开发离线、实时数据融合分析的数据大屏应用。
Linux入门到精通
本套课程是从入门开始的Linux学习课程,适合初学者阅读。由浅入深案例丰富,通俗易懂。主要涉及基础的系统操作以及工作中常用的各种服务软件的应用、部署和优化。即使是零基础的学员,只要能够坚持把所有章节都学完,也一定会受益匪浅。
相关文章
|
3月前
|
Python
【Python 自动化】小说推文一键生成思路概述
【Python 自动化】小说推文一键生成思路概述
43 0
|
3月前
|
XML 存储 JSON
Python学习 -- 常用数据交换格式(CSV、XML、JSON)
Python学习 -- 常用数据交换格式(CSV、XML、JSON)
31 0
|
4天前
|
机器学习/深度学习 自然语言处理 语音技术
《Python 语音转换简易速速上手小册》第1章 Python语音处理概述(2024 最新版)(下)
《Python 语音转换简易速速上手小册》第1章 Python语音处理概述(2024 最新版)
28 0
|
1月前
|
XML 测试技术 API
Python下的XML文件处理技巧与实践
【2月更文挑战第2天】 Python下的XML文件处理技巧与实践
55 0
|
1月前
|
XML 安全 API
Python读写XML文件:深入解析与技术实现
Python读写XML文件:深入解析与技术实现
48 0
|
1月前
|
Python
python线性回归概述
python线性回归概述
8 0
|
1月前
|
XML 数据格式 Python
Python生成XML文件
Python生成XML文件
14 0
|
3月前
|
XML JavaScript 数据格式
python - bs4提取XML/HTML中某个标签下的属性
python - bs4提取XML/HTML中某个标签下的属性
28 0
|
3月前
|
Python
Python 生成sitemap.xml
Python 生成sitemap.xml
16 0
|
4月前
|
XML 存储 JavaScript
【python】DOM模块读写XML文件
【python】DOM模块读写XML文件
23 0