I am using jackson to serialize customer entities in XML files with the below code :
XmlMapper mapper = new XmlMapper();
String exportPath = System.getenv("TFH_HOME") + File.separator + "data" + File.separator + "XML" + File.separator;
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
List<Customer> customers = customerService.findAllExplicit();
customers.forEach((customer) ->
{
try
{
mapper.writeValue(new File(exportPath + "customers" + File.separator + customer.getCustomerNumber() + ".xml"), customer);
}
catch (IOException ex)
{
logger.error("Error serializing customer " + customer.toString() + " : " + ex.getMessage());
}
});
This works perfectly and creates an XML file per customer with all of its data. The issue is that this data is in french, therefore contains accentuated characters such as é. This is the code i use to then de-serialize said customers :
public void importCustomers()
{
File customerFolder = new File(exportPath + "customers");
for (File customerFile : customerFolder.listFiles())
{
try
{
String customerXML = inputStreamToString(new FileInputStream(customerFile));
Customer customer = mapper.readValue(customerXML, Customer.class);
customer.setId(null);
customerService.save(customer);
}
catch (IOException ex)
{
logger.error("Error importing customer : " + ex.getMessage());
}
}
}
private static String inputStreamToString(InputStream is) throws IOException
{
StringBuilder sb = new StringBuilder();
String line;
try (BufferedReader br = new BufferedReader(new InputStreamReader(is)))
{
while ((line = br.readLine()) != null)
{
sb.append(line);
}
}
return sb.toString();
}
It works perfectly except the é characters are converted to É when de-serializing (they are serialized OK and the resulting XML file shows the correct characters). I know this has to do with the character encoding (UTF8 versus ISO-8859-2) but i do not know how to wire this into the Jackson de-serializing mechanism.
Any help would be appreciated!
Thanks
I ended up ditching Jackson and using the java.beans.XMLDecoder;
class instead. Accentuated characters are preserved when using this method.