使用python读取标记中包含xmlns的XML文件

时间:2012-04-26 12:31:00

标签: python xml xml-namespaces

我正在尝试从xml文件中读取元素以添加新元素。

我想找的标签包含xmlns。

看起来像这样:

<labcore.logging.service defaultSystemIdentificationCode="??" xmlns="http://schemas.com/labcore/configuration">
<storage databaseName="COMIT" storageType="Oracle" />
<logEventDefinitions>

            <logEventDefinition assembly="IM.LogEventDefinitions" />                    
            <logEventDefinition assembly="BarcodeEntry.LogEventDefinitions" />                  
            <logEventDefinition assembly="MaintenanceScheduling.ActivityLog.Messages.LogEventDefinitions" />
            <logEventDefinition assembly="InCore.LogEventDefinitions" />    


  <logEventDefinition assembly="LogEventPackage.LogEventDefinitions" />
</logEventDefinitions>

我的python代码如下所示:

import xml.etree.ElementTree as xml

def modifyComitLoggingConfigFile(filePath, fileName):

    path = filePath+"\\"+fileName

    IM = xml.Element("logEventDefinition")
    IM.attrib["assembly"] = "IM.LogEventDefinitions"

    BarcodeEntry = xml.Element("logEventDefinition")
    BarcodeEntry.attrib["assembly"] = "BarcodeEntry.LogEventDefinitions"

    MaintenanceScheduling = xml.Element("logEventDefinition")
    MaintenanceScheduling.attrib["assembly"] = "MaintenanceScheduling.ActivityLog.Messages.LogEventDefinitions"

    RTCosmo3 = xml.Element("logEventDefinition")
    RTCosmo3.attrib["assembly"] = "InCore.LogEventDefinitions"

    tree = xml.parse(path)
    rootElement = tree.getroot()

    loggingTag = rootElement.find("labcore.logging.service")
    print "loggingTag"
    print loggingTag
    print

    logEventDefinitionsTag = loggingTag.find("logEventDefinitions")
    print "logEventDefinitionsTag"
    print logEventDefinitionsTag
    print

    logEventDefinitionsTag.insert(0, RTCosmo3)
    logEventDefinitionsTag.insert(0, MaintenanceScheduling)
    logEventDefinitionsTag.insert(0, BarcodeEntry)        
    logEventDefinitionsTag.insert(0, IM)

    print "definitionfilesTag"
    print logEventDefinitionsTag
    print

    print "xml.tostring of definitionfilesTag"
    print xml.tostringlist(logEventDefinitionsTag)
    print

    tree.write(path+"1")
    return

并在以下行:

import xml.etree.ElementTree as xml

def modifyComitLoggingConfigFile(filePath, fileName):

    path = filePath+"\\"+fileName

    IM = xml.Element("logEventDefinition")
    IM.attrib["assembly"] = "IM.LogEventDefinitions"

    BarcodeEntry = xml.Element("logEventDefinition")
    BarcodeEntry.attrib["assembly"] = "BarcodeEntry.LogEventDefinitions"

    MaintenanceScheduling = xml.Element("logEventDefinition")
    MaintenanceScheduling.attrib["assembly"] = "MaintenanceScheduling.ActivityLog.Messages.LogEventDefinitions"

    RTCosmo3 = xml.Element("logEventDefinition")
    RTCosmo3.attrib["assembly"] = "InCore.LogEventDefinitions"

    tree = xml.parse(path)
    rootElement = tree.getroot()

    loggingTag = rootElement.find("labcore.logging.service")
    print "loggingTag"
    print loggingTag
    print

    logEventDefinitionsTag = loggingTag.find("logEventDefinitions")
    print "logEventDefinitionsTag"
    print logEventDefinitionsTag
    print

    logEventDefinitionsTag.insert(0, RTCosmo3)
    logEventDefinitionsTag.insert(0, MaintenanceScheduling)
    logEventDefinitionsTag.insert(0, BarcodeEntry)        
    logEventDefinitionsTag.insert(0, IM)

    print "definitionfilesTag"
    print logEventDefinitionsTag
    print

    print "xml.tostring of definitionfilesTag"
    print xml.tostringlist(logEventDefinitionsTag)
    print

    tree.write(path+"1")
    return

并在以下行中:

loggingTag = rootElement.find("labcore.logging.service")

发生以下错误:

AttributeError: 'NoneType' object has no attribute 'find'

但是当我从标签中删除xmlns部分时,它可以正常工作。有谁知道我怎么解决这个问题?

2 个答案:

答案 0 :(得分:3)

尝试使用xml.etree.ElementTree.register_namespace(prefix, uri)

所以

xml.etree.ElementTree.register_namespace("lc", "http://schemas.com/labcore/configuration")

然后,当您搜索元素时,请使用元素名称前面的前缀

loggingTag = rootElement.find("lc:labcore.logging.service")

答案 1 :(得分:1)

正如我的评论中所述,这是一个有效的解决方案。但我现在不确定,如果服务仍然有用,也许我可以今天或星期一进行测试。

解决方案是: 将名称空间添加到变量:

namespace = "{http://schemas.com/labcore/configuration}"

然后当你调用find时,在搜索标记之前添加{0}并格式化。

loggingTag = rootElement.find("{0}labcore.logging.service".format(namespace))

这是你必须为所有孩子添加的。

我不确定服务是否会在那之后起作用,因为他为每个标签添加了一个ns:标记名。 但我认为它会起作用。

root元素现在是:

<configuration xmlns:ns0="http://schemas.com/labcore/configuration">
孩子现在是:

<ns0:labcore.logging.service defaultSystemIdentificationCode="??">

如果有效则会给出反馈。但希望它会:)