Not sure if you still have the problem...
The requirements for a KML file can be found here: https://developers.google.com/kml/documentation/kmlreference
and the xml schema for GPX here: http://www.topografix.com/GPX/1/1/gpx.xsd
I did not 'neg' you, but I guess some people did it as these are pretty easy searches ;)
You are indeed right that you have to write it to a file which you can then publish to Google Earth via an intent.
Good luck