0

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.

1
Contributor
1
Reply
3
Views
8 Years
Discussion Span
Last Post by foosion
0

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)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.