如何使用 minidom 读取具有属性的 XML 标记的值

发布于 2024-12-11 20:55:06 字数 400 浏览 5 评论 0原文

我有一个如下所示的 XML。

<nb:myelement param='oxygen'>Value</nb:element>

我正在使用以下 python 代码。

for sub in dom.getElementsByTagName('nb:myelement'):
   if(sub.getAttributeNode("param").nodeValue == 'oxygen'):
      value = sub.getElementsByTagName('nb:myelement')[0].toxml()

但最后一行抛出一个索引超出范围的执行。如何获取包含在具有属性的标签中的“值”?

I have an XML which looks like this.

<nb:myelement param='oxygen'>Value</nb:element>

I am using the following python code.

for sub in dom.getElementsByTagName('nb:myelement'):
   if(sub.getAttributeNode("param").nodeValue == 'oxygen'):
      value = sub.getElementsByTagName('nb:myelement')[0].toxml()

But the last line is throwing an index out of range execption. How do I get the 'value' enclosed by a tag that also has attributes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离去的眼神 2024-12-18 20:55:06

您的 nb:myelement 中没有其他 nb:myelement 元素。所以最后一行实际上是

[][0].toxml()

如果 nb:myelement 中只有文本,则只需使用该元素的文本节点

sub.firstChild.nodeValue

There are no other nb:myelement elements in your nb:myelement. So last line is really

[][0].toxml()

If there is only text in nb:myelement, just use text node of this element

sub.firstChild.nodeValue
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文