<s> import xml . dom . minidom <EOL> import re <EOL> class EasyXML : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , name ) : <EOL> '''<STR_LIT>''' <EOL> self . _parent = None <EOL> self . _name = name <EOL> self . _elements = [ ] <EOL> self . _attributes = { } <EOL> self . _element_map = { } <EOL> def __getattr__ ( self , name ) : <EOL> '''<STR_LIT>''' <EOL> if name . startswith ( '<STR_LIT:_>' ) : <EOL> return object . __getattr__ ( self , name ) <EOL> if name in self . _element_map : <EOL> return self . _element_map [ name ] <EOL> element = EasyXML ( name ) <EOL> element . _parent = self <EOL> return element <EOL> def __call__ ( self , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> e = new_element = EasyXML ( self . _name ) <EOL> e . _parent = self . _parent <EOL> e . _attributes = kwargs <EOL> while e . _parent and e not in e . _parent . _element_map . values ( ) : <EOL> e . _parent . _elements . append ( e ) <EOL> e . _parent . _element_map [ e . _name ] = e <EOL> e = e . _parent <EOL> return new_element <EOL> def __str__ ( self ) : <EOL> '''<STR_LIT>''' <EOL> def to_xml ( obj ) : <EOL> element = doc . createElement ( obj . _name ) <EOL> for k in obj . _attributes : <EOL> element . setAttribute ( k , str ( obj . _attributes [ k ] ) ) <EOL> for e in obj . _elements : <EOL> element . appendChild ( to_xml ( e ) ) <EOL> return element <EOL> doc = xml . dom . minidom . Document ( ) <EOL> doc . appendChild ( to_xml ( self ) ) <EOL> return re . sub ( r'<STR_LIT>' , '<STR_LIT>' , doc . toprettyxml ( indent = '<STR_LIT:U+0020>' ) ) . strip ( ) </s>
