This is probably as good as you can get with minidom.
However, consider ditching minidom--it's really only there when you absolutely, postively need some kind of DOM api and only have the standard library. Note the documentation for minidom.
Users who are not already proficient with the DOM should consider using the xml.etree.ElementTree module for their XML processing instead
Warning: The xml.dom.minidom module is not secure against maliciously constructed data. If you need to parse untrusted or unauthenticated data see XML vulnerabilities.
XML in Python is almost always processed with the ElementTree interface, not a DOM interface. There are many implementations of ElementTree including xml.etree.ElementTree (pure Python, in stdlib) and xml.etree.cElementTree (CPython, in stdlib), and lxml (third-party all-singing, all-dancing xml processing library that uses libxml2).
Here is how I would do this:
try:
# On Python 2.x, use the faster C implementation if available
from xml.etree import cElementTree as ET
except ImportError:
# pure-python fallback
# In Python3 just use this, not the one above:
# Python3 will automatically choose the fastest implementation available.
from xml.etree import ElementTree as ET
xmlstr = """<root>
<skill>
<id>14</id>
<name>C++</name>
</skill>
<skill>
<id>15</id>
<name>Java</name>
</skill>
</root>"""
root = ET.fromstring(xmlstr)
def get_subelem_texts(elem, subelems):
"""Return {subelem: textval,...} or None if any subelems are missing (present but empty is ok)"""
attrs = {}
for sa in skillattrs:
textval = skill.findtext(sa)
if textval is None:
return None
attrs[sa] = textval.strip()
return attrs
skillattrs = 'id name'.split()
for skill in root.find('skill'):
args = get_subelem_texts(skill, skillattrs)
if args is not None:
my_object.create(**args)