我试图从xml文件中读取一个元素来添加新元素.
我试图找到的标签包含xmlns.
它看起来像这样:
我的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
logEventDefinitionsTag = loggingTag.find("logEventDefinitions")
print "logEventDefinitionsTag"
print logEventDefinitionsTag
logEventDefinitionsTag.insert(0, RTCosmo3)
logEventDefinitionsTag.insert(0, MaintenanceScheduling)
logEventDefinitionsTag.insert(0, BarcodeEntry)
logEventDefinitionsTag.insert(0, IM)
print "definitionfilesTag"
print logEventDefinitionsTag
print "xml.tostring of definitionfilesTag"
print xml.tostringlist(logEventDefinitionsTag)
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
logEventDefinitionsTag = loggingTag.find("logEventDefinitions")
print "logEventDefinitionsTag"
print logEventDefinitionsTag
logEventDefinitionsTag.insert(0, RTCosmo3)
logEventDefinitionsTag.insert(0, MaintenanceScheduling)
logEventDefinitionsTag.insert(0, BarcodeEntry)
logEventDefinitionsTag.insert(0, IM)
print "definitionfilesTag"
print logEventDefinitionsTag
print "xml.tostring of definitionfilesTag"
print xml.tostringlist(logEventDefinitionsTag)
tree.write(path+"1")
return
并在以下行:
loggingTag = rootElement.find("labcore.logging.service")
发生以下错误:
AttributeError: 'NoneType' object has no attribute 'find'
但是当我从标签中删除xmlns部分时,它可以工作.有谁知道我怎么解决这个问题?