python对象中的意外属性错误

时间:2014-07-01 20:27:27

标签: python xml python-2.7 suds

我正在解析suds客户端从Web Services SOAP API返回的对象

我有一个attributeObjects列表,比如

(defectStateAttributeValueDataObj){
   attributeDefinitionId = 
      (attributeDefinitionIdDataObj){
         name = "Comment"
      }
   attributeValueId = 
      (attributeValueIdDataObj){
         name = "Owner changed because of default owner assignment specified in the component map"
      }
}

(defectStateAttributeValueDataObj){
   attributeDefinitionId = 
      (attributeDefinitionIdDataObj){
         name = "OwnerName"
      }
   attributeValueId = 
      (attributeValueIdDataObj){
         name = "Schmoe, Joe"
      }
 }

我使用以下循环来提取键/值对:

for defect in myDefectsPage.mergedDefects :
   print defect.cid,
   for attribute in defect.defectStateAttributeValues:
       print attribute
       attr= attribute.attributeDefinitionId.name
       val=attribute.attributeValueId.name
       print attr,'=',val,
       print ""     

(上述对象是print属性命令的结果)

这将按照EVERY的预期工作,除了attribute.attributeDefinitionId.name ==" Comment"

对于那个我得到了

追踪(最近一次通话):   文件,第63行,in     VAL = attribute.attributeValueId.name AttributeError:' Text'对象没有属性' name'

这很奇怪,因为如果我使用的话     val = attribute.attributeValueId#.name 它会打印

Commment =(attributeValueIdDataObj){              name ="由于组件映射中指定的默认所有者分配,所有者已更改"           }

所以看起来它是一个attributeValueIdDataObj并且DOES有一个名字属性。

我使用了suds DEBUG日志记录,无论attribute.attributeDefinitionId.name是什么,XML返回元素看起来都完全相同。

我不知道它是如何变成一个' Text'尝试访问name属性时的对象

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

进一步检查(并在发生异常时打印出返回对象的类型)这是Web服务SOAP服务器中的一个错误。

当评论为空时,它返回

<attributeValueId/>

标签,

而不是正确的

 <attributeValueId>
      <name/>
 </attributeValueId>

对象。所以它导致sax.Text对象而不是suds.attributeValueIdDataObj对象

所以没有python或suds的谜团可以解决。

对不起误报......

相关问题