Python XML Minidom在子节点中逐标记地获取元素

时间:2021-01-08 22:39:08

I am curently working on a IRC Bot and want to retrieve the configuration from an XML file that look like this :

我正在研究一个IRC机器人,我想从一个XML文件中检索配置,它看起来像这样:

<server>
  <host> HOST1 </host>
  <port> 6667 </port>
  <channel>
    <name> CHANNAME1</name>
  </channel>
  <channel>
    <name> CHANNAME2 </name>
  </channel>
</server>
<server>
  <host> HOST2 </host>
  <port> 6667 </port>
  <channel>
    <name> CHANNAME3 </name>
  </channel>
</server>

And my code look like this :

我的代码是这样的:

doc = minidom.parse(xml)
node = doc.documentElement
servers = doc.getElementsByTagName("server")

for server in servers:

    channels = server.getElementsByTagName("channel")
    host = server.getElementsByTagName("host")[0].childNodes[0].data
    print host

    for channel in channels:
        NAME = channel.getElementsByTagName("name")[0].childNode[0].data
        print NAME

And the output is

和输出

HOST1
CHANNAME1
CHANNAME2
CHANNAME3
HOST2
CHANNAME1
CHANNAME2
CHANNAME3

But all I need is

但我所需要的是

HOST1
CHANNAME1
CHANNAME2
HOST2
CHANNAME3

Is there a way to get all the elements with the tag name "channel" within my node server instead of the whole xml file ?

是否有一种方法可以在我的节点服务器中,而不是整个xml文件中,使用标记名“通道”获取所有元素?

2 个解决方案

#1


4  

Your code looks correct as is. You have childNode when it should be childNodes in the NAME assignment, but I'm assuming that is just a typo in your question.

您的代码看起来是正确的。你有一个childNode,它在名称分配中应该是childNodes,但是我假设这只是你问题中的一个错误。

Your XML isn't valid though. You need to have some kind of root node wrapping the servers. As it's currently written, I wouldn't expect that to even parse successfully. It should look something like this:

但是XML不是有效的。您需要使用某种根节点包装服务器。就像它目前所写的那样,我不指望它能成功地解析。它应该是这样的:

<servers>
  <server>
    <host> HOST1 </host>
    <port> 6667 </port>
    <channel>
      <name> CHANNAME1</name>
    </channel>
    <channel>
      <name> CHANNAME2 </name>
    </channel>
  </server>
  <server>
    <host> HOST2 </host>
    <port> 6667 </port>
    <channel>
      <name> CHANNAME3 </name>
    </channel>
  </server>
</servers>

With that XML, and the code you've provided, I get the exact output you expect.

有了这个XML和您提供的代码,我得到了您所期望的输出。

#2


3  

Don't use the minidom. Use the ElementTree API instead. It can handle subtree searches much better:

不要使用minidom。使用ElementTree API代替。它可以更好地处理子树搜索:

from xml.etree import ElementTree as ET

doc = ET.parse(xmlfile).getroot()

for server in doc.findall('server'):
    host = server.find('./host').text
    print host
    for channel in server.findall('channel'):
        name = channel.find('name').text
        print name

#1


4  

Your code looks correct as is. You have childNode when it should be childNodes in the NAME assignment, but I'm assuming that is just a typo in your question.

您的代码看起来是正确的。你有一个childNode,它在名称分配中应该是childNodes,但是我假设这只是你问题中的一个错误。

Your XML isn't valid though. You need to have some kind of root node wrapping the servers. As it's currently written, I wouldn't expect that to even parse successfully. It should look something like this:

但是XML不是有效的。您需要使用某种根节点包装服务器。就像它目前所写的那样,我不指望它能成功地解析。它应该是这样的:

<servers>
  <server>
    <host> HOST1 </host>
    <port> 6667 </port>
    <channel>
      <name> CHANNAME1</name>
    </channel>
    <channel>
      <name> CHANNAME2 </name>
    </channel>
  </server>
  <server>
    <host> HOST2 </host>
    <port> 6667 </port>
    <channel>
      <name> CHANNAME3 </name>
    </channel>
  </server>
</servers>

With that XML, and the code you've provided, I get the exact output you expect.

有了这个XML和您提供的代码,我得到了您所期望的输出。

#2


3  

Don't use the minidom. Use the ElementTree API instead. It can handle subtree searches much better:

不要使用minidom。使用ElementTree API代替。它可以更好地处理子树搜索:

from xml.etree import ElementTree as ET

doc = ET.parse(xmlfile).getroot()

for server in doc.findall('server'):
    host = server.find('./host').text
    print host
    for channel in server.findall('channel'):
        name = channel.find('name').text
        print name