python读取xml文件属性和属性值

一般的通过属性名查找属性值在百度上很多。例如http://www.jb51.net/article/50812.htm

以下记录以下不知道属性名时候直接查找属性名和属性值的方法:

以下代码是从http://www.thinksaas.cn/ask/question/23572/一个回答中看到的,自己加了点注释。

import xml.dom.minidom
from xml.dom import Node

dom = xml.dom.minidom.parse(‘test.xml‘)
root = dom.documentElement

for child in root.childNodes:
 if child.nodeType == Node.ELEMENT_NODE:  # 是否是元素节点
     dictAttr = {}
     for key in child.attributes.keys():    # child.attrbutes.keys()查看所有属性,返回一个列表
     attr = child.attributes[key]          # 返回属性地址
     dictAttr[attr.name] = attr.value    # attr.name为属性名  attr.value为属性值
     listInfos.append({child.nodeName: dictAttr})

  自己在win7下测试结果:

XML文件test.xml  :

cmd:

时间: 2024-08-27 16:06:23

python读取xml文件属性和属性值的相关文章

python 读取xml文件

首先,获得标签信息abc.xml <?xml version="1.0" encoding="utf-8"?> <catalog> <maxid>4</maxid> <login username="pytest" password="123456"> <caption>Python</caption> <item id="4&

python读取xml、html文件

自动化测试设计测试用例时有的用例需要重复执行很多次,此时就需要对测试使用到的测试值进行参数化设计,而且参数化有利于测试用例的后期维护:并且自动化web测试也需要xpath进行页面元素的定位,所以我学习下python如何处理xml文件. 以下为使用到的html文件,把他放在了程序父文件夹的configure文件夹下 <html> <head> <title>通讯录</title> <meta http-equiv="content-type&q

python读取xml文件报错ValueError: multi-byte encodings are not supported

1.在使用python对xml文件进行读取时,提示ValueError: multi-byte encodings are not supported 很多贴子上说把xml的编码格式改为,就可以正常执行了 <?xml version="1.0" encoding="utf-8"?> 但是事实证明,不成功,解决方法 1.用记事本方式打开xml文件,encoding后面指的就是编码方式 2.把你的xml文件另外为utf-8 在进行读取,文件编码问题解决 原文

Android布局文件layout.xml的一些属性值

第一类:属性值 true或者 false android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 android:layout_centerInparent 相对于父元素完全居中 android:layout_alignParentBottom 贴紧父元素的下边缘 android:layout_alignParentLeft 贴紧父元素的左边缘 android:layout_alignParentRight 贴

[转]Android布局文件layout.xml的一些属性值

第一类:属性值 true或者 false android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 android:layout_centerInparent 相对于父元素完全居中 android:layout_alignParentBottom 贴紧父元素的下边缘 android:layout_alignParentLeft 贴紧父元素的左边缘 android:layout_alignParentRight 贴

python+appium获取app元素属性值

元素的属性我们经常会用到,当定位到某个元素后,有时会需要用到这个元素的text值.className.resource-id.checked等. 一般标准的属性我们都可以通过get_attribute("属性名称")来获取,我们来看看下面截图的元素都是怎么获取的吧.从上到下来看.我们从text开始讲,我们先通过xpath方式定位到这个元素 获取text方法有:虽然有两种方法,但一般都用第一种,因为写法比较简单.知道有第二种方法就好了.获取resource-id值方法: 获取classn

C#读取XML文件并取值

1.新建XML文件: <?xml version="1.0" encoding="utf-8" ?> <SystemInfo> <Class name="News" desc="文章栏目"> <Item name="NewsInfo" desc="新闻资讯">1</Item> <Item name="MediaC

Python读取xml报错解析--ExpatError: not well-formed (invalid token)

xml文件内容如代码所示存入的名字为login.xml: <?xml version="1.0" encoding="utf-8"?> <info> <explain>126</explain> <url>http://www.126.com</url> <null username="" password="">请先输入您的邮箱帐号</

@Value()读取配置文件属性,读出值为null的问题

一.问题描述 自定义一个Filter如下: @Component public class JwtFilter extends GenericFilterBean{ @Value("${jwt.header}") private String header; //此处无法注入 值为 null 在config中将此filter注册给spring @Configuration @AutoConfigureAfter(JwtFilter.class) public class JwtConf