AttributeError: 'Nonetype' Objekt hat kein Attribut 'nodeValue'
-
22-10-2019 - |
Frage
Ich verwende KSOAP, um zwischen einer Android -App und dem Python -Server mit den folgenden Dateien zu kommunizieren. Ich versuche, alle Werte in der geposteten XML -Datei abzurufen. Aber ich bekomme immer wieder, AttributeError: 'NoneType' object has no attribute 'nodeValue'
. Kann mir jemand sagen, was mit dem Code los ist, als ich versuchte, den Fehler zu debuggen, aber dennoch nicht zu tun.
Teil der XML -Datei (nur MacFilterList und Map -Knoten können leer sein):
<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
bezieht sich auf den Dateinamen der XML -Datei.):
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 (wo die Klasse, wo die Klasse, XML_ProfileDataStore
ist):
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
Lösung
Dies bedeutet, dass eine Methode/ein Attribut zurückgegeben wurde None
, und Sie haben versucht, auf seine zuzugreifen nodeValue
Attribut. Entweder ist Ihr Algorithmus falsch oder Sie müssen für testen None
Vor dem Zugriff auf das Attribut. Entschuldigung, aber ich kann Ihnen nicht mehr helfen, ich habe diese Bibliothek noch nie benutzt.
Andere Tipps
Der Nichetype -Fehler erscheint aus verschiedenen Gründen. Das Problem besteht Zwei Möglichkeiten, dies zu tun: a. Fordern Sie ein Befehlszeilenargument an, das dazu führt, dass ein "Printline" -Flag wahr ist b. Fügen Sie brutal eine Zeile hinzu, um die Zeile zu drucken und dann zu entfernen oder zu kommentieren (einfacher)
(b) ist die einfache Möglichkeit, es schnell zu machen. Gehen Sie also zu Ihrer Datei po2prop.py und suchen Sie nach Zeilen:
for line in content.splitlines(True):
outputstr = self.convertline(line)
outputlines.append(outputstr)
return u"".join(outputlines).encode(self.encoding)
und fügen Sie diese Zeile im Schleifencode hinzu:
sys.stdout.write(outputstr)
So wird es (es wird im Code kommentiert und bei Bedarf erfasst):
for line in content.splitlines(True):
outputstr = self.convertline(line)
outputlines.append(outputstr)
# sys.stdout.write(outputstr)
return u"".join(outputlines).encode(self.encoding)
So einfach ist das. Hinweis: Vergiss nicht:
import sys
im Importabschnitt der Datei
Können Sie zunächst die Fehlermeldung veröffentlichen? Versuchen Sie dann, die Zeile in Ihrem Code zu isolieren und zum Debuggen etwas schmutzig zu verwenden print node, node.name
(oder ähnliches) Vor dieser Zeile, um den XML -Knoten zu identifizieren, der Ihren Schutz bricht.
Sie sollten dann in der Lage sein zu verstehen, warum diese Zeile ein Fall ist, den Sie nicht vorausgesehen haben.
Irgendwie funktioniert jetzt alles gut. Früher entferne ich die Knoten in der XML -Datei, die leere Elemente enthalten, und dies würde natürlich gut funktionieren, da ich herausfinden würde, dass das leere Element möglicherweise den Fehler verursacht. Jetzt ersetze ich jedoch die ursprüngliche XML -Datei zurück und Daten können abgerufen werden. Hier ist die Funktion der .py -Datei, die ich bearbeitet hatte, um nach leerem Element in der XML -Datei zu überprüfen.
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