python的Element.tagName不起作用 [英] Element.tagName for python not working

查看:47
本文介绍了python的Element.tagName不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在django中有以下代码,并且返回有关 tagName 属性的错误:

I have the following code in django and it's returning an error about tagName attribute:

def _parse_google_checkout_response(response_xml):
    redirect_url=''
    xml_doc=minidom.parseString(response_xml)
    root = xml_doc.documentElement
    node=root.childNodes[1]
    if node.tagName == 'redirect-url':
        redirect_url=node.firstChild.data
    if node.tagName == 'error-message':
        raise RuntimeError(node.firstChild.data)
    return redirect_url

以下是错误响应:

Exception Type: AttributeError
Exception Value:    
Text instance has no attribute 'tagName'

有人知道这里发生了什么吗?

Anyone have a clue as to what's going on here?

推荐答案

node=root.childNodes[1]

节点是DOM文本节点.它没有tagName属性.例如

node is a DOM Text node. It has no tagName attribute. e.g.

>>> d = xml.dom.minidom.parseString('<root>a<node>b</node>c</root>')
>>> root = d.documentElement
>>> nodes = root.childNodes
>>> for node in nodes:
...   node
...
<DOM Text node "u'a'">
<DOM Element: node at 0xb706536c>
<DOM Text node "u'c'">

在上面的示例中,文档元素(根")具有3个子节点.第一个是文本节点,它没有tagName属性.而是可以通过'data'属性访问其内容: root.childNodes [0] .data

In the example above, the document element('root') has 3 child nodes. The 1st one is a text node, it has no tagName attribute. Instead, it's content can be accessed by 'data' attribute: root.childNodes[0].data

第二个是元素,它包含其他节点.这种节点具有tagName属性.

The 2nd one is an element, it contains other nodes. Node of this kind has a tagName attribute.

第三个类似于第一个.

这篇关于python的Element.tagName不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆