通过名称而不是索引查找xml节点 [英] Finding an xml node by its name rather than by its index

查看:104
本文介绍了通过名称而不是索引查找xml节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过名称查找xml节点并在标签之间获取其值?

How do I find an xml node by its name and get its value between the tags?

我正在按照以下方式操作:

I'm doing that the following way:

from xml.dom import minidom
dom = minidom.parseString(ET.tostring(ET.fromstring(some_xml), "utf-8"))
self.a1 = dom.childNodes[0].childNodes[4].childNodes[0].nodeValue
self.a2 = dom.childNodes[0].childNodes[5].childNodes[0].nodeValue

我想使用标签名称而不是在数组 childNodes 中使用其索引来完成此操作。

I want to do that using the name of the tag instead of using its index in an array childNodes. How?

更新

<ReconnectResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://ccc.aaa.bbb/api/v1"> 
  <ErrorMessage /> 
  <ErrorCode>0</ErrorCode> 
  <ServerTime>aaa</ServerTime> 
  <OAuthToken>bbb</OAuthToken> 
  <OAuthTokenSecret>ccc</OAuthTokenSecret> 
</ReconnectResponse>

和代码:

dom.getElementsByTagName("ServerTime") # => []

update2

dom.toxml()
u'<?xml version="1.0" ?><ns0:ReconnectResponse xmlns:ns0="http://ccc.aaa.bbb/api/v1">\n  <ns0:ErrorMessage/>\n  <ns0:ErrorCode>0</ns0:ErrorCode>\n  <ns0:ServerTime>aaa</ns0:ServerTime>\n  <ns0:OAuthToken>bbb</ns0:OAuthToken>\n  <ns0:OAuthTokenSecret>ccc</ns0:OAuthTokenSecret>\n</ns0:ReconnectResponse>'

但是我如何获得价值?我尝试了这一点:

but how I get the value? I tried this:

dom.getElementsByTagName("ns0:OAuthToken")
[<DOM Element: ns0:OAuthToken at 0x10635a878>]
(Pdb) dom.getElementsByTagName("ns0:OAuthToken")[0]
<DOM Element: ns0:OAuthToken at 0x10635a878>
(Pdb) dom.getElementsByTagName("ns0:OAuthToken")[0].nodeValue
(Pdb) dom.getElementsByTagName("ns0:OAuthToken")[0].toxml()
u'<ns0:OAuthToken>aaaaaa</ns0:OAuthToken>'


推荐答案

您需要使用 getElementsByTagNameNS ,因为您没有名为 ServerTime 的标签,因此有一个名为 {http://ccc.aaa.bbb/api的标签/ v1} ServerTime (其中 {http://ccc.aaa.bbb/api/v1} 表示默认名称空间。)

You need to use getElementsByTagNameNS, because you don't have a tag named ServerTime, you have one named {http://ccc.aaa.bbb/api/v1}ServerTime (where {http://ccc.aaa.bbb/api/v1} indicates the default namespace.)

getElementsByTagNameNS("http://ccc.aaa.bbb/api/v1", "ServerTime")

由于文档元素的最后一个属性,此命名空间隐式添加到XML主体中的每个标签中:

This namespace is implicitly added to every tag in your XML body, due to the last property of the document element:

<ReconnectResponse ... xmlns="http://ccc.aaa.bbb/api/v1">

这篇关于通过名称而不是索引查找xml节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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