python lxml获取节点的名称

时间:2015-07-22 22:05:09

标签: python xml python-2.7 lxml

这是我的xml文件:

<FuzzyComparison>
    <Modules>
        <Module>
            <name>AutosoukModelMakeFuzzyComparisonModule</name>
            <configurationLoader>DefaultLoader</configurationLoader>
            <configurationFile>MakesModels.conf</configurationFile>
            <settings></settings>
        </Module>
        <Module>
            <name>DefaultFuzzyComparisonModule</name>
            <configurationLoader>DefaultLoader</configurationLoader>
            <configurationFile>Buildings.conf</configurationFile>
            <settings>
                <attribute>building</attribute>
            </settings>
        </Module>
    </Modules>
    </FuzzyComparison>

这是我尝试用以下代码解析它的代码:

from lxml import etree
class AttributesXMLParser():
    def __init__(self):
        self.doc=etree.parse('Items.xml')

    def getValueOfTag(self, tagName): #This function returns the value of a specific tag for exmaple, the tageName could be "FirstDate"
        return self.doc.find(tagName).text

    def loadFuzzySettings(self):
        modulesDict = list()
        modules = self.doc.findall('FuzzyComparison/Modules/Module')
        for module in modules:
            moduleDict = dict()
            moduleName = module.find('name').text
            moduleDict['name'] = moduleName
            moduleConfigurationLoader = module.find('configurationLoader').text
            moduleDict['configurationLoader'] = moduleConfigurationLoader
            moduleConfigurationFile = module.find('configurationFile').text
            moduleDict['moduleConfigurationFile'] = moduleConfigurationFile
            settings = module.findall('settings')
            settingsDict = dict()
            for oneSetting in settings:
                settingsDict[oneSetting] = oneSetting.text
            moduleDict['settings'] = settingsDict
            modulesDict.append(moduleDict)
        return modulesDict

这就是结果:

[{'moduleConfigurationFile': 'MakesModels.conf', 'configurationLoader': 'Default
Loader', 'name': 'AutosoukModelMakeFuzzyComparisonModule', 'settings': {<Element
 settings at 0x25257c8>: None}}, {'moduleConfigurationFile': 'Buildings.conf', '
configurationLoader': 'DefaultLoader', 'name': 'DefaultFuzzyComparisonModule', '
settings': {<Element settings at 0x2525e48>: '\n\t\t\t\t'}}]

我的问题

我不知道如何获取settings节点的名称和价值,因为除了settings之外,你看到一切都很有效,我需要这样:

"attribute": building

但是我的代码给了我:

{<Element settings at 0x2525e48>: '\n\t\t\t\t'}}]

你能帮忙解决一下吗?

1 个答案:

答案 0 :(得分:2)

由于mX1, mY1, mX2, mY2返回一个列表,您希望迭代该列表的元素内容,而不是列表本身。您还希望使用元素标记作为键,而不是使用元素本身。

findall()

或者,如果您只有一个settingsDict = {} for settingsNode in module.findall('settings'): for setting in settingsNode: settingsDict[setting.tag] = setting.text 标记,

settings

可以简化为:

settingsDict = {}
for setting in module.find('settings'):
    settingsDict[setting.tag] = setting.text