4

I am trying to read a element form a xml file to add new elements.

The tag I'm trying to find contains xmlns.

It looks like this:

<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>

and my python code looks like this:

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

and at the following line:

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

and on the following line:

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

the following error occurs:

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

but when i remove the xmlns part from the tag, it works. does anybody know how i can solve this?

pnuts
  • 58,317
  • 11
  • 87
  • 139
damir
  • 1,928
  • 1
  • 16
  • 26
  • 3
    I suggest that you try using lxml instead (it's largely the same interface). – Marcin Apr 26 '12 at 12:59
  • i solved it, but im not sure if it will work. i have to add the namespace and put it before every tag im trying to find and format it. i will put the code in about 7 hours :) because i can not answer my own question, i dont have 100 reputation points :( Marcin, thanks for your tip, but i have to use the internal libs. – damir Apr 26 '12 at 13:09
  • Not directly related, but http://stackoverflow.com/questions/24876855/using-fromstring-with-lxml-prefixes addresses another aspect of namespace pain with lxml – Pat Mar 11 '15 at 22:16

2 Answers2

3

try using xml.etree.ElementTree.register_namespace(prefix, uri)

So

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

Then when you're searching for elements, use the prefix before the element name

loggingTag = rootElement.find("lc:labcore.logging.service")
hcayless
  • 1,036
  • 6
  • 7
  • 1
    no this is not working @hcayless. Error: raise SyntaxError("prefix %r not found in prefix map" % prefix) SyntaxError: prefix 'lc' not found in prefix map – damir Apr 27 '12 at 05:54
  • do you have an idea what it could be? because your solution looks much better :) – damir Apr 27 '12 at 05:59
  • 2
    ahh.. i got it, this feature is new in Python 3.2. Im using 2.7.2, thats the reason. so your solution should also work. my works with 2.7.2. thank you very much.. – damir Apr 27 '12 at 06:08
1

As in my comment, here is one solution that worked. but im not sure now, if the service will still work, maybe i can test it today or on monday.

solution is: add namespace to a variable:

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

and then when you call find.. add {0} before the searching tag and format it.

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

this you have to add for all childs.

im not sure if the service will work after that because he puts to every tag a ns:tagname. but i think it would work.

root element is now:

<configuration xmlns:ns0="http://schemas.com/labcore/configuration">

childs are now:

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

If will give feedback if it worked or not. but hopefully it will :)

damir
  • 1,928
  • 1
  • 16
  • 26