How can I generate a XML request file with the structure below using Simple?
<?xml version="1.0" encoding="UTF-8"?>
<ft>
<request clientId="123" apiName="api_search_location_stops_nearby" apiVersion="2.0">
<client clientId="123"/>
<requestType>api_search_location_stops_nearby</requestType>
<outputCoords>WGS84</outputCoords>
<fromCoordName>WGS84</fromCoordName>
<fromType>coords</fromType>
<fromWgs84Lat>48.22</fromWgs84Lat>
<fromWgs84Lon>16.39</fromWgs84Lon>
</request>
</ft>
To generate the part <request> ... </request>
is easy, but how can I add the XML open tag <?xml version="1.0" encoding="UTF-8"?>
and the <ft>
and the end tag </ft>
?
If you are already able to generate the <request>...</request>
tag, I assume that you already have a Request
object with the correct fields and so on, so in fact it is really simple..
@Root
public class Ft {
@Element
private Request request;
// constructor, getter, setter if needed
}
In order to generate the XML prolog you need to create your own Formatter/Serializer
with the Format(String)
constructor:
Serializer serializer = new Persister(//
new Format("<?xml version=\"1.0\" encoding= \"UTF-8\" ?>"));
Ft ft = new Ft();
ft.setRequest(myRequest);
serializer.write(ft, new File("ft.xml"));