#NEWLINE# def serialize_to_xml( value ) : #NEWLINE# #INDENT# if ( value is None ) : #NEWLINE# #INDENT# node = etree . Element ( 'null' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( value , bool ) : #NEWLINE# #INDENT# node = etree . Element ( 'literal' ) #NEWLINE# node . text = ( 'true' if value else 'false' ) #NEWLINE# node . attrib [ 'type' ] = 'boolean' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( value , ( basestring , int , float ) ) : #NEWLINE# #INDENT# node = etree . Element ( 'literal' ) #NEWLINE# try : #NEWLINE# #INDENT# node . text = unicode ( value ) #NEWLINE# #UNINDENT# except UnicodeDecodeError : #NEWLINE# #INDENT# node . text = unicode ( value , 'utf-8' ) #NEWLINE# #UNINDENT# node . attrib [ 'type' ] = [ d [ 1 ] for d in _XML_DATATYPES if isinstance ( value , d [ 0 ] ) ] [ 0 ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( value , dict ) : #NEWLINE# #INDENT# if ( '_type' in value ) : #NEWLINE# #INDENT# node = etree . Element ( 'object' , { #NEWLINE# #INDENT# 'type' : value [ '_type' ] , #NEWLINE# 'pk' : unicode ( value . get ( '_pk' , '' ) ) , #NEWLINE# #UNINDENT# } ) #NEWLINE# del value [ '_type' ] #NEWLINE# del value [ '_pk' ] #NEWLINE# if ( '_url' in value ) : #NEWLINE# #INDENT# node . attrib [ 'url' ] = value [ '_url' ] #NEWLINE# del value [ '_url' ] #NEWLINE# #UNINDENT# if value . get ( '_terse' ) : #NEWLINE# #INDENT# node . attrib [ 'terse' ] = 'true' #NEWLINE# del value [ '_terse' ] #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# node = etree . Element ( 'collection' , { #NEWLINE# #INDENT# 'type' : 'mapping' , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# for key in value : #NEWLINE# #INDENT# v = serialize_to_xml ( value [ key ] ) #NEWLINE# subnode = etree . Element ( 'item' , { #NEWLINE# #INDENT# 'key' : key , #NEWLINE# #UNINDENT# } ) #NEWLINE# subnode . append ( v ) #NEWLINE# node . append ( subnode ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( value , ( list , tuple , set , frozenset ) ) : #NEWLINE# #INDENT# for ( x , y ) in ( ( list , 'list' ) , ( tuple , 'tuple' ) ) : #NEWLINE# #INDENT# if isinstance ( value , x ) : #NEWLINE# #INDENT# node = etree . Element ( 'collection' , { #NEWLINE# #INDENT# 'type' : y , #NEWLINE# #UNINDENT# } ) #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# node = etree . Element ( 'collection' , { #NEWLINE# #INDENT# 'type' : 'set' , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# for item in value : #NEWLINE# #INDENT# v = serialize_to_xml ( item ) #NEWLINE# subnode = etree . Element ( 'item' ) #NEWLINE# subnode . append ( v ) #NEWLINE# node . append ( subnode ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# node = etree . Element ( 'unknown' ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# return node