Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. from lxml import etree
  2.  
  3. def add_child(root, field, value, optional=False):
  4. if not value and optional: # if this is an optional field and no value is provided, skip writing the tag altogether
  5. return
  6.  
  7. if not value: # if the value is None, set it to an empty unicode object
  8. value = u''
  9.  
  10. if isinstance(value, int): # convert integers to string
  11. value = str(value)
  12.  
  13. child = etree.SubElement(root, field)
  14. child.text = value
  15. multipackid = "onethreethreeseven"
  16. multipackroot = etree.Element("Multipack")
  17. add_child(multipackroot, "id", multipackid)
  18.  
  19. string_io = StringIO.StringIO(
  20. etree.tostring(multipackroot, pretty_print=True))
  21. for thing in string_io:
  22. print thing
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement