3

私はksoapを使用して、投稿された次のファイルを含むAndroidアプリとPythonサーバーの間で通信しています。投稿されたXMLファイルのすべての値を取得しようとしています。しかし、私は取得し続けます、AttributeError: 'NoneType' object has no attribute 'nodeValue'。エラーをデバッグしようとしましたが、それでも失敗したため、コードの何が問題になっているのか誰かに教えてもらえますか?

XMLファイルの一部(MacFilterListとMapノードのみを空にすることができます):

<ProfileList>
<Profile>
    <ProfileName>Lab1</ProfileName>
    <Owner>admin</Owner>
    <Map>Lab</Map>
    <Visible>True</Visible>
    <MacFilterList>
        <string>00:14:BF:9F:5D:3A</string>
        <string>00:14:BF:9F:5D:52</string>
        <string>00:14:BF:9F:5D:37</string>
        <string>00:14:BF:9F:5D:43</string>
    </MacFilterList>
</Profile>
    .
    .
</ProfileList>

soapAPI.py(PROFILE_XMLxmlファイルのファイル名を参照します。):

def __init__(self):  
    self.profileFile = Config.PROFILE_XML
    self.profile = XML_ProfileDataStore()
    self.profile.LoadXMLFile(self.profileFile) 
               .
               .
def GetAllProfileData(self):
    self.profileFile = Config.PROFILE_XML
    self.profile.LoadXMLFile(self.profileFile) 
    result = self.profile.GetAllProfileData()
    return result 

profileData.py(クラスはどこにありますかXML_ProfileDataStore):

def GetAllProfileData(self):

    #Get a node list containing nodes with name Location
    ProfileList = self.XMLdoc.getElementsByTagName('Profile')
    NumArgCheck = 0
    profiles=""

    #For each location node in list
    for profileNode in ProfileList:
        #For each child nodes in Location node, compare the XY coordinates
        for ChildNode in profileNode.childNodes:
            #If child node has profile name profile_name
            if (cmp(ChildNode.nodeName, 'ProfileName') == 0):
                NumArgCheck += 1
                profiles = profiles + ChildNode.firstChild.data + ","
                ChildNode = ChildNode.nextSibling
                profiles = profiles + ChildNode.firstChild.nodeValue + ","
                ChildNode = ChildNode.nextSibling
                profiles = profiles + ChildNode.firstChild.nodeValue + ","
                ChildNode = ChildNode.nextSibling
                profiles = profiles + ChildNode.firstChild.nodeValue
                ChildNode = ChildNode.nextSibling

                for child in ChildNode.childNodes:
                   profiles = profiles + "," + child.firstChild.nodeValue
                profiles = profiles+";"

    return profiles
4

4 に答える 4

3

これは、何らかのメソッド/属性が を返しNone、そのnodeValue属性にアクセスしようとしたことを意味します。アルゴリズムが間違っているかNone、属性にアクセスする前にテストする必要があります。申し訳ありませんが、それ以上のことはできません。私はこのライブラリを使用したことがありません。

于 2011-09-04T15:39:53.280 に答える
1

NoneType エラーは、さまざまな理由で表示されます。問題は、どの「行」がエラーを引き起こしているかを知るためのハードコーディングされた方法がないことです...私がしたことは、「printline」オプションを導入するために、po2prop.pyファイルを少しいじることでした...それには 2 つの方法があります。「printline」フラグを true にするコマンド ライン引数を要求します。 b. 行を印刷するために残酷に行を追加してから、それを削除するかコメントします(より簡単です)

(b) は簡単にすばやく実行できる方法なので、po2prop.py ファイルに移動して次の行を検索します。

    for line in content.splitlines(True):
        outputstr = self.convertline(line)
        outputlines.append(outputstr)
    return u"".join(outputlines).encode(self.encoding)

ループコードに次の行を追加します。

        sys.stdout.write(outputstr)

したがって、次のようになります(コードでコメント化されているため、必要に応じてコメントを解除します):

    for line in content.splitlines(True):
        outputstr = self.convertline(line)
        outputlines.append(outputstr)
    #   sys.stdout.write(outputstr)
    return u"".join(outputlines).encode(self.encoding)

それと同じくらい簡単です。ヒント: 忘れないでください:

    import sys

ファイルのインポートセクションで

于 2012-09-15T14:26:38.273 に答える
0

まず、エラーメッセージを公開していただけますか? 次に、コード内の行を分離し、デバッグのprint node, node.nameために、この行の前にダーティ (または類似のもの) を使用して、保護を破っている XML ノードを特定します。

そうすれば、なぜこの行が予測できなかったのかを理解できるはずです。

于 2011-09-05T08:50:13.583 に答える
0

どういうわけか、すべてがうまく機能しています。以前は、空の要素を含む XML ファイルのノードを削除しましたが、空の要素がエラーの原因である可能性があることがわかったので、もちろん正常に機能していました。ただし、元の XML ファイルを元に戻すと、データを取得できるようになりました。XMLファイル内の空の要素をチェックするために編集した.pyファイルの関数を次に示します。

    def GetAllProfileData(self):

    #Get a node list containing nodes with name Location
    ProfileList = self.XMLdoc.getElementsByTagName('Profile')
    NumArgCheck = 0
    profiles=""


    #For each location node in list
    for profileNode in ProfileList:
        #For each child nodes in Location node, compare the XY coordinates
        for ChildNode in profileNode.childNodes:
            #If child node has profile name profile_name
            if (cmp(ChildNode.nodeName, 'ProfileName') == 0):
                NumArgCheck += 1
                #If element is empty
                if ChildNode.firstChild is not None:
                    profiles = profiles + ChildNode.firstChild.nodeValue + ","
                else:
                    profiles = profiles + "EMPTY,"

                ChildNode = ChildNode.nextSibling

                if ChildNode.firstChild is not None:
                    profiles = profiles + ChildNode.firstChild.nodeValue + ","
                else:
                    profiles = profiles + "EMPTY,"

                ChildNode = ChildNode.nextSibling

                if ChildNode.firstChild is not None:
                    profiles = profiles + ChildNode.firstChild.nodeValue + ","
                else:
                    profiles = profiles + "EMPTY,"

                ChildNode = ChildNode.nextSibling

                if ChildNode.firstChild is not None:
                    profiles = profiles + ChildNode.firstChild.nodeValue
                else:
                    profiles = profiles + "EMPTY"

                ChildNode = ChildNode.nextSibling

                if ChildNode.firstChild is not None:
                    for child in ChildNode.childNodes:
                        profiles = profiles + "," + child.firstChild.nodeValue
                else:
                    profiles = profiles + ",EMPTY"

        profiles = profiles+";"

    return profiles
于 2011-09-05T15:17:19.733 に答える