简体   繁体   English

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

[英]Python XML Minidom Get element by tag in child node

I am curently working on a IRC Bot and want to retrieve the configuration from an XML file that look like this : 我正在努力研究IRC Bot并希望从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文件中获取标签名称为“channel”的所有元素?

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 ,它应该是NAME赋值中的childNodes ,但我认为这只是你问题中的一个错字。

Your XML isn't valid though. 但您的XML无效。 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: 它应该看起来像这样:

<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,以及你提供的代码,我得到了你期望的确切输出。

Don't use the minidom. 不要使用minidom。 Use the ElementTree API instead. 请改用ElementTree API It can handle subtree searches much better: 它可以更好地处理子树搜索:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM