,python,python-3.x,xml"/>

首页 > 解决方案 > 如何从python中获取xml标签中的单个元素

问题描述

  1. 项目清单

[在此处输入图像描述][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>

标签: pythonpython-3.xxml

解决方案


您可以使用该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")]

推荐阅读