AttributeError:“NoneType”对象没有属性“nodeValue”

2024-06-08 11:28:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用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

Tags: 文件inselfnodechildstringxmlprofile
3条回答

首先,请发布错误消息好吗?然后,尝试隔离代码中的这一行,为了进行调试,在这一行之前使用一些脏的print node, node.name(或类似的东西),以便识别破坏保护的XML节点。

你应该能够理解为什么这条线是一个你没有预见到的情况。

这意味着某些方法/属性返回了None,并且您试图访问其nodeValue属性。 要么你的算法是错误的,要么你需要在访问属性之前测试None。 对不起,我帮不了你更多的忙,我从来没有用过这个图书馆。

由于各种原因出现非类型错误。问题是没有硬编码的方法来知道是什么“行”导致了错误。。。 我所做的,是在po2prop.py文件中播放一点,以便引入一个“printline”选项。。。 有两种方法: a、 请求将导致“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

在文件的导入部分

相关问题 更多 >