我试图找到的标签包含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")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。