1 / 4
文档名称:

使用Python处理XML.docx

格式:docx   大小:48KB   页数:4页
下载后只包含 1 个 DOCX 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

使用Python处理XML.docx

上传人:86979448 2017/12/2 文件大小:48 KB

下载得到文件列表

使用Python处理XML.docx

文档介绍

文档介绍:使用Python处理XML

使用Python单纯解析XML的最基本的方法就是直接使用Python自带的minidom库。
比如有如下XML文件:
<?xml version=""?>
<root>
<singlenode>This is a single node</singlenode>
<multinode>This is first one</multinode>
<multinode>This is second one</multinode>
<multinode>This is third one</multinode>
<complexNode name="complex">
<childnode>This is a child node</childnode>
</complexNode>
</root>
使用如下代码获取里面每一项内容:
from import minidom
if __name__ == '__main__':
try:
xmldoc = ("")

singleNode = ("singlenode")[0]
print("Data of singleNode: %s"%())

multiNodes = ("multinode")
for multiNode in multiNodes:
print("Data of multiNode: %s"%())
complexNode = ("complexNode")[0]
print("The name attribute plexNode: %s"%(("name")))

childNode = ("childnode")[0]
print("Data of childNode: %s"%())
except:
print("Error: Parse XML file failed.")
输出结果如下:
Data of multiNode: This is second one
Data of multiNode: This is third one
The name attribute plexNode: ComplexNode
Data of childNode: This is a child node

同样可以直接使用Python自带的库来生成和保存XML文件。比如要生成上例使用的XML文件:
from import minidom
import codecs
if __name_