Is there any API to convert my TXT to GPX?
Is there a different method for exporting GPX files?
Sure that's possible with OpenLayers 3. You dont need to create an TXT first and then convert it to the GPX. In OpenLayers 3 there is a function called
ol.format.GPX() which will convert your features to a gpx format. With
writeFeatures() you can add you features to the ol gpx object. Here is a working JS FIDDLE.