Trying to parse xml to text I got something like this,
INPUT FILE
<Item id="1"></Item>
<Item id="2"></Item>
<Item id="1"></Item>
<Item id="2"></Item>
CURRENT OUTPUT
Item ->1
Item ->2
Item ->2
Item ->1
My DESIRED OUTPUT would be,
Item ->1
Item ->2
(Ignoring repeated id values)
The current code I'm using to obtain my CURRENT OUTPUT is,
list = node.getElementsByTagName('Item')
for item in list:
output_id = item.getAttribute('id')
print "Item ->", output_id
I've tried thousands of list remove methods but they all output the double ids. Help would be greatly appreciated. Tks
At first, every DOM parser will return doubled ids as they're different elements.
To avoid, go through dom tree and store results in dict
object. This will get you only last items.
UPD:
list = node.getElementsByTagName('Item')
items = {}
for item in list:
output_id = item.getAttribute('id')
items[output_id] = item # Put items into dict to use them later.
for id in items:
print "Item[%d] -> %s" % (id, items[id]) # Only single item per id left.
And more 'pythonic' way:
list = node.getElementsByTagName('Item')
items = dict((item.getAttribute('id'), item) for item in list)
for id in items:
print "Item[%d] -> %s" % (id, items[id]) # Only single item per id left.