编程语言
首页 > 编程语言> > 使用python读取标记中包含xmlns的XML文件

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

作者:互联网

我试图从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部分时,它可以工作.有谁知道我怎么解决这个问题?

解决方法:

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

所以

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

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

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

标签:python,xml,xml-namespaces
来源: https://codeday.me/bug/20190626/1290783.html