I'm trying to parse xml, but am missing something. In the following, I can get the key, but not the value

from xml.dom import minidom

xmlstring = '''<search ver="3.0">
	<loc id="USAR0433" type="1">Paris, AR</loc>
	<loc id="USID0192" type="1">Paris, ID</loc>
</search>'''

dom = minidom.parseString(xmlstring)
r = dom.getElementsByTagName('search')[0]

res = []
for x in r.childNodes:
	v = x.toxml()
	for j in x.childNodes:
		k = j.nodeValue
		res.append({k:v})
for r in res:
	for k,v in r.items():
		print(k + ': ' + v)

which results in:

Paris, AR: <loc id="USAR0433" type="1">Paris, AR</loc>
Paris, ID: <loc id="USID0192" type="1">Paris, ID</loc>

I'd like to extract "USAR0433".

I could easily get it with re or string parsing, but wonder what minidom function to use to retrieve it. Parsing the string from toxml() seems to be cheating.

Figured it out

from xml.dom import minidom

xmlstring = '''<search ver="3.0">
	<loc id="USAR0433" type="1">Paris, AR</loc>
	<loc id="USID0192" type="1">Paris, ID</loc>
</search>'''

dom = minidom.parseString(xmlstring)
loclist = dom.getElementsByTagName('loc')
res = {}
for l in loclist:
	v = l.attributes['id'].value
	k = l.childNodes[0].nodeValue
	res[k] = v

for k,v in res.items():
	print(k + ': ' + v)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.