找到具有给定值的xml-tag

时间:2015-10-02 09:28:46

标签: xml python-2.7

我有一个具有以下结构的XML文件

<?xml version="1.0" encoding="ISO-8859-1"?>
<application name="MyApp">
    <component name="MyComponent">
        <setting>
            <name>mySetting</name>
            <value>myValue</value>
        </setting>
    </component>
</application>

现在,我想在MyComponent中选择名称为mySetting的设置(仅限一个)。我试过了root.findall('./application/component/[@name="mySetting"]'),但这给了我NoneType

编辑:当前代码如下所示

import xml.etree.ElementTree as ET
xml = """<?xml version="1.0" encoding="ISO-8859-1"?>
    <application>
        <component name="MyComponent">
            <setting>
                <name>mySetting</name>
                <value>myValue</value>
            </setting>
        </component>
    </application>"""
root = ET.fromstring(xml)
settings = root.findall('./component/setting')

有人有解决方案吗?

1 个答案:

答案 0 :(得分:1)

我终于找到了一个答案,因为@mguijarr更容易,因为我预计会从xPath中省略root节点:

post_max_size