Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from lxml import etree
- def add_child(root, field, value, optional=False):
- if not value and optional: # if this is an optional field and no value is provided, skip writing the tag altogether
- return
- if not value: # if the value is None, set it to an empty unicode object
- value = u''
- if isinstance(value, int): # convert integers to string
- value = str(value)
- child = etree.SubElement(root, field)
- child.text = value
- multipackid = "onethreethreeseven"
- multipackroot = etree.Element("Multipack")
- add_child(multipackroot, "id", multipackid)
- string_io = StringIO.StringIO(
- etree.tostring(multipackroot, pretty_print=True))
- for thing in string_io:
- print thing
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement