Python XML Minidom Get element by tag in child node

user2153627 picture user2153627 · Jun 30, 2013 · Viewed 35.7k times · Source

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

<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 ?

Answer

James Holderness picture James Holderness · Jun 30, 2013

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.

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:

<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.