4

新しい要素を追加するために、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、タグからパーツを削除すると、機能します。誰かが私がこれを解決する方法を知っていますか?

4

2 に答える 2

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")
于 2012-04-26T17:17:56.933 に答える
1

私のコメントのように、これがうまくいった解決策の1つです。しかし、今はわかりません。サービスがまだ機能する場合は、今日または月曜日にテストできます。

解決策は次のとおりです。変数に名前空間を追加します。

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

次に、find ..を呼び出すときに、検索タグの前に{0}を追加してフォーマットします。

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

これは、すべての子に追加する必要があります。

彼はすべてのタグにns:tagnameを付けているので、その後サービスが機能するかどうかはわかりません。しかし、私はそれがうまくいくと思います。

ルート要素は次のようになります。

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

子供たちは今:

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

それがうまくいったかどうかフィードバックを与えるかどうか。しかし、うまくいけばそれは:)

于 2012-04-27T05:58:42.850 に答える