python lxml库如何创建带冒号TAG名的XML文件
由于没有系统学习过xml格式,但是想要使用python的lxml库实现一个xml文件的生成,由于该格式比较特殊,根TAG的名称中带了一个冒号,形如:
<msg:note mlns:msg="http://sample.com/test">
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</msg:note>
对于xml格式一知半解的我通过搜索引擎进行搜索带冒号的TAG相关关键词,看了许多答案并未获得比较清晰的解答,最终找到官方教程得以解答:
链接: lxml tutorial
具体解决思路:
1.针对这个问题首先我们需要知道:
带冒号的TAG名是什么
XML格式一般形态
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>
2.那么带冒号的TAG是什么呢?
这是xml格式的命名空间的一种写法。
3.那么命名空间又是干什么的呢?
简单来说就是界定可能因为不同文件同样的TAG导致冲突的问题;例如文件A和文件B的根TAG都是root的时候,两者是不同的意思,那么此时可以通过命名空间的方式进行区分。
命名空间的结构如下:
<book:note xmlns:book="http://www.contoso.com">
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</book:note>
到这里之后,发现这个就和我需要的文件一致了。
4.那么如何使用python的lxml库对这个格式进行编码呢?
python lxml库创建带冒号TAG
详细编码及解释如下(python version >= 3.6,低于3.6的版本请自行修改格式化语法):
from lxml import etree
# 定义命名空间URI
NAMESPACE = "http://www.contoso.com"
# 定义XML格式的命名空间格式
NSMAP = {"book": "{}".format(NAMESPACE)}
# 创建根TAG,这里需要对参数 nsmap进行赋值,按照上述格式进行传值即可
# 这里的{{{}}}是为了对format语法进行转义,可以直接替换为如下更直观的写法:
# root_tag = etree.Element("{http://www.contoso.com}note", nsmap=NSMAP)
root_tag = etree.Element("{{{}}}note".format(NAMESPACE), nsmap=NSMAP)
# 创建其他子TAG
to_tag = etree.SubElement(root_tag, "to")
from_tag = etree.SubElement(root_tag, "from")
heading_tag = etree.SubElement(root_tag, "heading")
body_tag = etree.SubElement(root_tag, "body")
# 给所有TAG赋值
to_tag.text = "George"
from_tag = "John"
heading_tag = "Reminder"
body_tag = "Don't forget the meeting!"
# 打印生成的XML结构
print(etree.tostring(root_tag))
这里对命名空间的编码进一步解释:
如何定义命名空间:
# 这里是给XML格式定义命名空间
# 参数含义和格式: nsmap={"命名空间名": "命名空间URI"}
etree.Element("{http://www.contoso.com}note", nsmap={"book": "http://www.contoso.com"})
如何给对应TAG增加命名空间:
# 想要给对应tag前增加命名空间需要增加如下格式:
# TAG名参数格式: {命名空间URI}TAG名
# 给根TAG增加命名空间语法:
etree.Element("{http://www.contoso.com}note", nsmap={"book": "http://www.contoso.com"})
# 给子TAG增加命名空间语法:
to_tag = etree.SubElement(root_tag, "{http://www.contoso.com}to")
至此,关于lxml库创建带命名空间的tag创建OK。