Search code examples
pythonxmlminidom

Python minidom xml parsing to get the value of first child node


I am using python minidom to parse a xml, but not able to get it working for below xml. I want to select the first server tag and want the value of name tag , in this case "Server1"

<class>
<name>MyClass</name>
<security>
<name>MyClass</name>
</security>
<server>
<name>Server1</name>
<ssl>
 <name>Server1</name>
</ssl>
<server-info>
 <name>Server1</name>
</server-info>
</server>
<server>
<name>Server2</name>
<ssl>
 <name>Server2</name>
</ssl>
<server-info>
 <name>Server2</name>
</server-info>
</server>
<server>
<name>Server3</name>
<ssl>
 <name>Server3</name>
</ssl>
<server-info>
 <name>Server3</name>
</server-info>
</server>
</class>

Solution

  • You'll have to reference the XML DOM documentation and grit your teeth.

    To get the first <server> element, then its <name>:

    from xml.dom import minidom
    
    document = minidom.parse(inputfilename)
    
    def findChildNodeByName(parent, name):
        for node in parent.childNodes:
            if node.nodeType == node.ELEMENT_NODE and node.localName == name:
                return node
        return None
    
    def getText(nodelist):
        rc = []
        for node in nodelist:
            if node.nodeType == node.TEXT_NODE:
                rc.append(node.data)
        return ''.join(rc)
    
    # Get the first of all `<server>` nodes
    server = document.getElementsByTagName('server')[0]
    
    # Get the first child node that is a `<name>` element
    name = findChildNodeByName(server, 'name')
    if name is not None:
        print getText(name.childNodes)