AttributeError: 'NoneType'对象没有'nodeValue'属性

3 投票
4 回答
17283 浏览
提问于 2025-04-17 01:24

我正在使用ksoap来让一个安卓应用和一个包含以下文件的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

4 个回答

0

首先,你能把错误信息发出来吗?然后,试着找出你代码中的那一行。为了调试,你可以在这一行之前加一些简单的 print node, node.name(或者类似的代码),这样可以帮助你找出哪个XML节点导致了问题。

这样你就能明白为什么这一行会出现你没有预料到的情况。

1

NoneType错误出现的原因有很多。问题在于,我们没有固定的方法来知道是哪一行代码导致了这个错误……

我做的就是对po2prop.py文件进行了一些修改,添加了一个“printline”的选项……

有两种方法可以做到这一点:

a. 请求一个命令行参数,这样就可以让“printline”标志变为真

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

在文件的导入部分

3

这句话的意思是,有些方法或者属性返回了None,然后你试图去访问它的nodeValue属性。要么是你的算法有问题,要么就是在访问这个属性之前,你需要先检查一下是不是None。抱歉,我帮不了你更多,因为我从来没有用过这个库。

撰写回答