AttributeError:「NoneType」オブジェクトには属性「nodevalue」がありません
-
22-10-2019 - |
質問
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_XML
XMLファイルのファイル名を指します。):
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
解決
これは、何らかの方法/属性が返されることを意味します None
, 、そしてあなたはそれにアクセスしようとしました nodeValue
属性。アルゴリズムが間違っているか、テストする必要があります None
属性にアクセスする前に。申し訳ありませんが、私はそれ以上あなたを助けることはできません、私はこのライブラリを使用したことがありません。
他のヒント
NoneTypeエラーがさまざまな理由で表示されます。問題は、「ライン」がエラーを引き起こすものを知るためのハードコード化された方法がないことです...私がしたことは、「印刷ライン」オプションを導入するために、PO2Prop.pyファイルで少しプレイしたことでした...それを行う2つの方法:a。 「printline」フラグを真のbにするコマンドライン引数を要求します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
ファイルのインポートセクションで
まず、エラーメッセージを公開していただけますか?次に、コードのラインを分離し、デバッグするために汚れを使用してください print node, node.name
保護を破壊しているXMLノードを識別するために、この行の前の(または同様のもの)。
そうすれば、このラインがあなたが予測しなかったケースである理由を理解できるはずです。
どういうわけか、すべてが正常に機能しています。以前は、空の要素を含む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