python - 如何从python中获取xml标签中的单个元素
问题描述
- 项目清单
[在此处输入图像描述][1]。
如何在 python 中分别获取值 regname,type...。我需要分别获取类型值和地址。
for regfile in root.findall('regfile'):
reg_value = regfile.find('reg').text
print(reg_value)
for reg in regfile.findall('reg'):
name = reg.find('name').text
type = reg.find('text').text
我试过了,但它抛出了像“AttributeError:'NoneType'对象没有属性'text'”这样的错误
<regfile regfilename="blkreq" type="blk_required_v2_rdl_rf_t" num="1" addr="0x0" incr="0x8">
<name><![CDATA[blk_required_v2_rdl_rf_t]]></name>
<description><![CDATA[]]></description>
<property name="num" value="1"/>
<reg regname="uid_v2" type="block_uid_v2_t" num="1" addr="0x4" incr="0x4">
<name><![CDATA[Block Unique ID Type]]></name>
<description><![CDATA[The IPID and the Platform Type together provides Unique ID of an IP]]></description>
<property name="msbhigh" value="1"/>
<property name="num" value="1"/>
<property name="retention" value=""/>
<property name="width" value="32"/>
<property name="shared" value="0"/>
<property name="accesswidth" value="8"/>
<field fieldname="ipid">
<name><![CDATA[Design IP Identification]]></name>
<description><![CDATA[This is reserved for future use.]]></description>
<msb>31</msb>
<lsb>8</lsb>
<property name="hw_r" value="1"/>
<property name="reset" value="0h"/>
<property name="retention" value=""/>
<property name="sw_r" value="1"/>
<property name="width" value="0"/>
</field>
<field fieldname="platform">
<name><![CDATA[Platform]]></name>
<description><![CDATA[Implementation for the specific platform class (asic, fpga, vp, etc) - supplied
a global parameter, PLATFORM_TYPE, by the RTL and defined in the platform package in DVI_LIB.
The reset value shall reflect the configuration.]]></description>
<msb>3</msb>
<lsb>0</lsb>
<property name="hw_w" value="1"/>
<property name="reset" value=""/>
<property name="retention" value=""/>
<property name="sw_r" value="1"/>
<property name="width" value="0"/>
</field>
</reg>
解决方案
您可以使用该xml.etree.ElementTree
包将您的 xml 代码解析为一棵树,并以这种方式对其进行迭代:
# Official DOC:
# https://docs.python.org/3/library/xml.etree.elementtree.html#module-xml.etree.ElementTree
import xml.etree.ElementTree as ET
x = '''<?xml version="1.0"?>
<regs>
<reg regname="uid_v2" type="block_uid_v2_t" num="1" addr="0x4" incr="0x4"> </reg>
<reg regname="uid_v3" type="block_uid_v3_t" num="3" addr="0x4" incr="0x4"> </reg>
</regs>'''
# tree = ET.parse("some_file.xml")
tree = ET.fromstring(x) # I use a string here in this example
for child in tree:
print(child.tag, child.attrib)
是一个字典:你可以通过简单地child.attrib
获取特定的属性类型child.attrib["type"]
。查看此 repl.it 以获取实时示例。
编辑- 按照以下评论中的要求:
# Get only the relevant nodes from the tree
for child in tree:
if "type" in child.attrib:
if child.attrib["type"] == "block_uid_v2_t":
print(child.tag, child.attrib)
# List comprehension to store all relevant nodes in a list
regs = [child.attrib for child in tree if ("type" in child.attrib and child.attrib["type"]=="block_uid_v2_t")]
# Store only 'regname' of relevant nodes
reg_ids = [child.attrib["regname"] for child in tree if ("type" in child.attrib and child.attrib["type"]=="block_uid_v2_t")]
推荐阅读
- vue.js - 如何在 VUE 组件的 mapState 中调用“this”?
- java - java / spring中的@SessionAttribute丢失数据?
- javascript - node.js - 期望每个响应接收 2 个字节,但接收到整个字节字符串
- python - 在多线程中观察变量的 Pythonic 方式?
- android - Android Studio 4.0 布局检查器不工作
- linux - 检查 VM 的 Linux 命令是 azure VM 或 amazon EC2 实例
- amazon-web-services - AWS CLI - 在不同的 S3 存储桶之间移动文件
- c# - 自动映射器自定义解析器源成员到目标对象列表映射问题
- angular - Angulat,Http GET 错误(拆分)queryParams
- cmake - CPACK:如何在使用 CPACK 打包人文件中使用 CPACK_RPM_ADDITIONAL_MAN_DIRS 变量?