XML Serialization XML, Missing Fields in a File

This is a problem when you try to serialize a class below using the code below that I get is an xml file below without all the lines in the class.

Class (some static values ​​have changed, but basically this), I left all the generated get \ set, but they are all there with public access modifiers.

public class NotificationConfiguration implements Serializable { public static final String PORT_KEY = "mail.smtp.port"; public static final String DEFAULT_PORT_VALUE = "587"; public static final String TTL_KEY = "mail.smtp.starttls.enable"; public static final String DEFAULT_TTL_VALUE = "true"; public static final String AUTH_KEY = "mail.smtp.auth"; public static final String DEFAULT_AUTH_VALUE = "true"; public static final String MAIL_SERVER_KEY = "mail.smtp.host"; public static final String DEFAULT_MAIL_CLIENT_HOST = "smtp.gmail.com"; public static final String DEFAULT_MAIL_CLIENT_USERNAME = "*********"; public static final String DEFAULT_MAIL_CLIENT_PASSWORD = "*********"; public static final String DEFAULT_MAIL_CLIENT_ADDRESS = "*********"; public static final String DEFAULT_ADMIN_EMAIL = "*********"; public static final long DEFAULT_MAIL_INTERVAL = 24*60*60*1000; //One time a day default public static final String SAVED_FOLDER_NAME = "C:\\Library"; public static final String SAVED_FILE_NAME = "C:\\Library\\NotificationCfg.xml"; private String portValue = DEFAULT_PORT_VALUE; private String ttlValue = DEFAULT_TTL_VALUE; private String authValue = DEFAULT_AUTH_VALUE; private String mailClientHost = DEFAULT_MAIL_CLIENT_HOST; private String mailClientUserName = DEFAULT_MAIL_CLIENT_USERNAME; private String mailClientPassword = DEFAULT_MAIL_CLIENT_PASSWORD; private String mailClientAddress = DEFAULT_MAIL_CLIENT_ADDRESS; private String adminEMail = DEFAULT_ADMIN_EMAIL; private boolean overdueSubsNotificationEnabled = false; private boolean adminReportNotificationEnabled = false; private long mailInterval = } 

Code used for serialization, which also creates a folder if missing.

 public void storeChanges() { try { try { File f = new File(NotificationConfiguration.SAVED_FOLDER_NAME); f.mkdir(); } catch (Exception e){} XMLEncoder encoder = new XMLEncoder( new BufferedOutputStream(new FileOutputStream(NotificationConfiguration.SAVED_FILE_NAME))); encoder.writeObject(notificationConfig); encoder.close(); System.out.println(LOG_CONFIGURATION_STORED); } catch (Exception ex) { System.out.println(LOG_CONFIGURATION_NOT_STORED + ex.getMessage()); } } 

The resulting XML file with no exceptions that occur during serialization. It basically has great value.

+4
source share
4 answers

XMLEncoder encodes information on how to recover an object. If field values ​​have not changed from their default values, XMLEncoder does not store anything.

This can cause confusion.

Therefore, my basic rules when using XMLEncoder are:
1. Do not initialize the fields. do not do private String foo = DEFAULT_FOO;
2. Do nothing in the default constructor.
3. You have another method or factory that will give you the default setting.

+4
source

I highly recommend reading the XMLEncoder Javadoc again. I will point out the main differences to the binary serialization that we all know about.

  • to restore an instance that needs a class definition available to the JVM
  • It only serializes data. And only changed from the default data.
  • The result of the 2 points above is that there is no reason to serialize static final values ​​- they are part of the class definition.

Binary serialization, on the other hand, serializes a class definition and can load a class from a byte stream that was not previously available to the JVM.

That's why you got the results you see. Well, this is design behavior, and you are using it correctly. This seems to be just not what you need. By the way, see what Xstream has to offer.

+1
source

Could it be that only mailInterval has a getter?

I just looked at the question again, apparently there is a getter for all the fields, so ...

0
source

What is SAVED_FOLDER_NAME? Is it like a factory object, and did you accidentally call setMailInterval on that object?

0
source

Source: https://habr.com/ru/post/1301440/


All Articles