开发者

'NoneType' object has no attribute 'data'

I am 开发者_如何学Csending a SOAP request to my server and getting the response back. sample of the response string is shown below:

<?xml version = '1.0' ?>
<env:Envelope xmlns:env=http:////www.w3.org/2003/05/soap-envelop
.
..
..
<env:Body>
    <epas:get-all-config-resp xmlns:epas="urn:organization:epas:soap"> ^M
...
...
<epas:property name="Tom">12</epas:property>
> 
> <epas:property name="Alice">34</epas:property>
> 
> <epas:property name="John">56</epas:property>
> 
> <epas:property name="Danial">78</epas:property>
> 
> <epas:property name="George">90</epas:property>
> 
> <epas:property name="Luise">11</epas:property>

...
^M
</env:Body?
</env:Envelop>

What I noticed in the response is that there is an extra character shown in the body which is "^M". Not sure if this could be the issue. Note the ^M shown! when I tried parsing the string returned from the server to get the names and values using the code sample:

elements = minidom.parseString(xmldoc).getElementsByTagName("property")      
myDict = {}
for element in elements:
  myDict[element.getAttribute('name')] = element.firstChild.data

But, I am getting this error: 'NoneType' object has no attribute 'data'. May be its something to do with the "^M" shown on the xml response back!

Any ideas/comments would be appreciated,

Cheers


[Edited to make clearer, and to suggest looking for an empty element]

Apparently, some of the elements returned by getElementsByTagName don't have a firstChild. This happens when the element is empty, as in

<epas:property name="Empty"></epas:property>

When minidom encounters that situation, it'll set "element.firstChild" to None. This is very likely what's happening to you.

Otherwise, it's hard to say what's happening, exactly, with only a fragment of the XML (and a broken one, at that), but you could try catching the exception and inspecting the element in question:

for element in elements:
    try:
        myDict[element.getAttribute('name')] = element.firstChild.data
    except AttributeError:
        print element, element.firstChild

Or, instead of simply printing the element, you could call the debugger (import pdb; pdb.set_trace()). Then you can see the element, and understand why it's giving you trouble.

BTW, the "^M" is simply a windows-style carriage-return. I adapted the xml fragment you pasted, to test locally, and the "^M" makes no difference whatsoever, minidom takes care of it.

So, check for an empty element, or use the try/except as I suggested. If you still can't tell what's going on, paste the complete XML string (at http://pastebin.com/, for example), I might be able to help.

Also, on a related note: once you've sorted out this issue, you can construct the dictionary with a list comprehension:

myDict = dict((element.getAttribute('name'), element.firstChild.data) for element in elements)

And, if you've determined that it is a matter of empty elements, you can skip them thusly:

myDict = dict((element.getAttribute('name'), element.firstChild.data) for element in elements if element.firstChild is not None)


You could filter elements which the first child is None, it seems to be about the ^M indeed, it is probably being turned into a TextNode object, a blank one without data.

elements = minidom.parseString(xmldoc).getElementsByTagName("property")      
myDict = {}
for element in elements:
    if element.firstChild:
        myDict[element.getAttribute('name')] = element.firstChild.data


The error indicates that one of your elements has a firstChild that has been set to None, and therefore trying to access its .data results in an error.

I suspect that the trailing ^M is the problem. The element produced for the ^M is invalid, and/so it has no firstChild element.

You can get around this by checking to see whether firstChild is equal to None before you try to extract its .data member.


element.firstChild is None. Are you sure you don't want element.data? I would guess firstChild refers to the first child element of element rather than a text node.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜