属性错误:“NoneType”对象没有属性“nodeValue”;
我正在使用 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
I am using ksoap to communicate between an android app and the python server containing the following files posted. I am trying to retrieve all the values in the XML file posted. But i keep getting, AttributeError: 'NoneType' object has no attribute 'nodeValue'
. Can anyone tell me what's wrong with the code as I tried to debug the error but still failed to do so.
Portion of the XML file (only MacFilterList and Map node can be empty):
<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
refers to the filename of the xml file.):
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 (where the class, XML_ProfileDataStore
is):
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这意味着某些方法/属性返回了
None
,并且您尝试访问其nodeValue
属性。您的算法可能是错误的,或者您需要在访问属性之前测试
None
。抱歉,我无法为您提供更多帮助,我从未使用过这个库。
It means that some method/attribute returned
None
, and you tried to access itsnodeValue
attribute.Either your algorithm is wrong, or you need to test for
None
before accessing the attribute.Sorry but I can't help you more than that, I have never used this library.
NoneType 错误的出现有多种原因。问题是没有硬编码的方法来知道什么“行”导致错误......
我所做的是稍微使用 po2prop.py 文件,以引入“printline”选项......
有两种方法可以做到这一点:
一个。请求一个命令行参数,这将导致“printline”标志为真
b.残酷地添加一行来打印该行,然后删除它或注释它(更容易)
(b)是快速完成此操作的简单方法,因此转到您的 po2prop.py 文件并搜索行:
并将此行添加到循环中code:
于是就变成了(代码里有注释,需要的时候取消注释):
就这么简单。
提示:不要忘记:
在文件的导入部分
NoneType error appears for various reasons. The problem is that there is no hardcoded way to know what "line" causes the error...
What I did, was play a little with the po2prop.py file, in order to introduce a "printline" option...
There are two ways to do that:
a. Request a command line argument which will cause a "printline" flag to be true
b. Brutally add a line to print the line and then remove it or comment it (easier)
(b) is the easy way to do it fast, so go to your po2prop.py file and search for lines:
and add this line in the loop code:
So it becomes (it is commented in the code, uncomment it when needed):
As simple as that.
HINT: DON'T FORGET TO:
at the import section of the file
首先,您能发布一下错误信息吗?然后,尝试隔离代码中的该行,并且为了进行调试,请在此行之前使用一些脏的
print 节点、node.name
(或类似的内容),以便识别正在损坏的 XML 节点你的保护。然后你应该能够理解为什么这条线是你没有预见到的情况。
First, could you please publish the error message? Then, try to isolate the line in your code, and for debugging, use some dirty
print node, node.name
(or something similar) before this line, in order to identify the XML node that is breaking your protection.You should then be able to understand why this line is a case you did not foresee.
不知怎的,现在一切都运转良好。以前,我删除了 XML 文件中包含任何空元素的节点,当然,当我发现空元素可能导致错误时,这会正常工作。但是,现在我替换回原来的 XML 文件并且可以检索数据。以下是我编辑的 .py 文件中的函数,该函数用于检查 XML 文件中的空元素。
Somehow everything is working fine now. Previously, i remove the nodes in the XML file which contain any empty elements and of course that would be working fine as i find out that the empty element might be causing the error. However, now i replace back the original XML file and data can be retrieved. Here is the function from the .py file which i had edited to check for empty element within the XML file.