2011-09-04 128 views
2

我正在使用kso​​ap在android應用程序和包含以下發布的文件的python服務器之間進行通信。我正在嘗試檢索發佈的XML文件中的所有值。但我不斷收到,AttributeError: 'NoneType' object has no attribute 'nodeValue'。任何人都可以告訴我代碼有什麼問題,因爲我試圖調試錯誤,但仍然沒有做到。AttributeError:'NoneType'對象沒有屬性'nodeValue'

部分XML文件(僅MacFilterList和地圖節點可以爲空):

<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 

回答

1

這意味着某些方法/屬性返回None,並且您試圖訪問其nodeValue屬性。 您的算法是錯誤的,或者您需要在訪問該屬性之前測試None。 對不起,但我不能幫你更多,我從來沒有使用過這個庫。

+0

一些在XML文件中的元素的進口部分是空元素,。然而,在我執行一些編碼來檢查這種情況後,錯誤仍然存​​在。而且我還在服務器中創建了一個.py文件來檢索數據,而不是使用我能夠這樣做的Android客戶端應用程序,但不是應用程序。 – user918197

0

首先,你能否發佈錯誤信息?然後,嘗試隔離代碼中的行,併爲了調試,在該行之前使用一些骯髒的print node, node.name(或類似的東西),以便識別破壞您的保護的XML節點。

你應該能夠理解爲什麼這條線是你沒有預見到的情況。

+0

嗨,我剛剛添加了該問題的錯誤消息。 – user918197

0

不知何故,現在一切正常。之前,我刪除了XML文件中包含任何空元素的節點,當然這樣做會很好,因爲我發現空元素可能會導致錯誤。但是,現在我將原來的XML文件替換回來並可以檢索數據。這是我編輯的.py文件中的函數,用於檢查XML文件中的空元素。

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 
+0

Traceback(最近一次調用最後一次): 文件「C:\ Users \ Qingyan \ Desktop \ Server \ SOAPpy \ Server.py」,行407,位於d o_POST fr = apply(f,ordered_args,named_args) File 「C:\ Users \ Qingyan \ Desktop \ Server \ WifiPositionSoapAPI.py」,行123 ,GetAllProfileData result = self.profile.GetAllProfileData() 文件「C:\ Users \ Qingyan \ Desktop \ Server \ ProfileDataStore.py 「,第153行,i n GetAllProfileData profiles = profiles + ChildNode.firstChild.nodeValue +」,「 AttributeError:'NoneType'對象沒有屬性'nodeValue' – user918197

1

由於各種原因,會出現NoneType錯誤。問題是,沒有硬編碼的方式來知道什麼「行」導致錯誤... 我所做的,是玩po2prop.py文件,以便引入「印刷線」選項... 有兩種方法可以做到這一點: 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 

在文件