Python基于dom操作xml数据的方法示例
作者:安迪的小熊 时间:2023-02-15 10:18:30
本文实例讲述了Python基于dom操作xml数据的方法。分享给大家供大家参考,具体如下:
1、xml的内容为del.xml,如下
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<maxid>4</maxid>
<login username="pytest" passwd='123456'>
<caption>Python</caption>
<item id="4">
<caption>test</caption>
</item>
</login>
<item id="2">
<caption>Zope</caption>
</item>
</catalog>
2、python的代码如下
# -*- coding:utf-8 -*-
#! python3
#1、获得标签属性
print("#1、获得标签属性")
import xml.dom.minidom
dom = xml.dom.minidom.parse("del.xml") #打开xml文档
root = dom.documentElement #得到xml文档
print("nodeName:",root.nodeName) #
print("nodeValue:",root.nodeValue)
print("nodeType:",root.nodeType)
print("ELEMENT_NODE:",root.ELEMENT_NODE)
#2、获得子标签
print("#2、获得子标签")
bb = root.getElementsByTagName('maxid')
print(type(bb))
print(bb)
b = bb[0]
print(b.nodeName)
print(b.nodeValue)
#3、获取标签属性值
print("#3、获取标签属性值")
itemlist = root.getElementsByTagName('login')
item =itemlist[0]
print(item.getAttribute("username"))
print(item.getAttribute("passwd"))
itemlist = root.getElementsByTagName('item')
item = itemlist[0] #通过在itemlist中的位置区分
print(item.getAttribute("id"))
item_1 = itemlist[1] #通过在itemlist中的位置区分
print(item_1.getAttribute("id"))
#4、获得标签对之间的数据
print("#4、获得标签对之间的数据")
itemlist1 = root.getElementsByTagName('caption')
item1 = itemlist1[0]
print(item1.firstChild.data)
item2 = itemlist1[1]
print(item2.firstChild.data)
#5总结
# minidom.parse(filename)
# 加载读取XML文件
#
# doc.documentElement
# 获取XML文档对象
#
# node.getAttribute(AttributeName)
# 获取XML节点属性值
#
# node.getElementsByTagName(TagName)
# 获取XML节点对象集合
#
# node.childNodes # 返回子节点列表。
#
# node.childNodes[index].nodeValue
# 获取XML节点值
#
# node.firstChild
# # 访问第一个节点。等价于pagexml.childNodes[0]
3、运行结果如下:
#1、获得标签属性
nodeName: catalog
nodeValue: None
nodeType: 1
ELEMENT_NODE: 1
#2、获得子标签
<class 'xml.dom.minicompat.NodeList'>
[<DOM Element: maxid at 0x1dad800>]
maxid
None
#3、获取标签属性值
pytest
123456
4
2
#4、获得标签对之间的数据
Python
test
运行结果截图:
PS:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:
在线XML/JSON互相转换工具:
http://tools.jb51.net/code/xmljson
在线格式化XML/在线压缩XML:
http://tools.jb51.net/code/xmlformat
XML在线压缩/格式化工具:
http://tools.jb51.net/code/xml_format_compress
XML代码在线格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/q357010621/article/details/52334442
标签:Python,dom,xml
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
还不知道Anaconda是什么?读这一篇文章就够了
2022-02-19 01:36:50
![](https://img.aspxhome.com/file/2023/9/114639_0s.png)
详解python播放音频的三种方法
2021-12-16 09:19:11
用python实现面向对像的ASP程序实例
2023-05-19 04:20:55
利用python求积分的实例
2023-07-08 15:44:23
oracle横向纵向求和代码实例
2024-01-21 12:14:01
Python bisect模块原理及常见实例
2023-01-12 07:22:15
ASP将Excel数据导入到SQLServer的实现代码
2011-03-10 10:38:00
asp中access升级到sql server后要做的工作
2007-08-11 13:35:00
Jupyter Lab设置切换虚拟环境的实现步骤
2021-10-11 06:05:35
![](https://img.aspxhome.com/file/2023/4/133584_0s.png)
Python函数any()和all()的用法及区别介绍
2021-11-23 04:43:16
对Python 中矩阵或者数组相减的法则详解
2024-01-02 01:29:17
![](https://img.aspxhome.com/file/2023/1/97011_0s.jpg)
Python温度转换实例分析
2023-08-21 22:48:32
pip安装Python库时遇到的问题及解决方法
2023-06-20 14:00:01
![](https://img.aspxhome.com/file/2023/1/100961_0s.png)
asp.net下使用DbProviderFactories的数据库操作类
2024-01-16 15:34:02
vue.js如何在网页中实现一个金属抛光质感的按钮
2024-04-28 09:21:26
![](https://img.aspxhome.com/file/2023/8/133108_0s.png)
Go语言中slice作为参数传递时遇到的一些“坑”
2023-08-05 02:05:12
Java正则表达式匹配字符串并提取中间值的方法实例
2022-02-06 11:22:43
python 爬取腾讯视频评论的实现步骤
2021-06-19 03:57:58
![](https://img.aspxhome.com/file/2023/3/90343_0s.png)
Java使用JDBC连接数据库的详细步骤
2024-01-15 17:42:42
Python常用内置函数总结
2022-06-06 06:02:43