Skip to content
Browse files

When parsing XML into a dictionary, aggregate identical adjacent elem…

…ent values into a list, and update dictionary with key as element name and value as the aggregated list. For example, a post element can have multiple tag children. In this case, the dictionary will have a key with the tag name and list value with each tag's text.
  • Loading branch information...
1 parent 76eecfd commit 8a063b3b8b0782887a695152162c3a75db0d9026 @simeonwillbanks simeonwillbanks committed
Showing with 11 additions and 1 deletion.
  1. +11 −1 posterous/parsers.py
View
12 posterous/parsers.py
@@ -60,7 +60,17 @@ def __init__(self, parent_element):
else:
# finally, if there are no child tags, extract the text
value = set_type(tag, element.text.strip())
- self.update({tag: value})
+ if childrenNames.count(tag) > 1:
+ # there are multiple instances of this tag, so they
+ # must be grouped together
+ try:
+ # append this tags text to the tag's matching list
+ self[tag].append(value)
+ except KeyError:
+ # the first for this tag
+ self.update({tag: [value]})
+ else:
+ self.update({tag: value})
class XMLList(list):

0 comments on commit 8a063b3

Please sign in to comment.
Something went wrong with that request. Please try again.