Johz

Getting Sub-Elements

Jul 10th, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.67 KB | None | 0 0
  1. ## This scrap of code uses the ElementTree's 'Element' type.  It defines a function that checks if
  2. ## the provided element contains sub-elements (the list(element) phrase) and then returns all of the
  3. ## sub elements of each of the sub elements of the provided element.  Put your hands up if you've used
  4. ## the word element too many times already.  It returns the elements in dictionary form, so that my
  5. ## previously-made dictionary in the getShard() method can be update()d with the results from this function.
  6.  
  7. def getSubElements(element):
  8.     if list(element):
  9.         return [getSubElements(i) for i in list(element)]
  10.     else:
  11.         return {element.tag: element.text}
Advertisement
Add Comment
Please, Sign In to add comment