简体   繁体   English

将对象保存为XML格式还是序列化?

[英]Save an object to XML format or with serialization?

I am developing an application in Java, where I have some classes, like Person and Company . 我正在用Java开发应用程序,其中有一些类,例如PersonCompany All these classes have some instances eg 所有这些类都有一些实例,例如

public class Person{
      private String firstName;
      private String lastName;
      private String mail;

     //more code..
}

So in some point I create an Object type of Person, which I want to save it for future use. 因此,在某种程度上,我创建了Person的Object类型,我想将其保存以备将来使用。 I am new to this, so I have this question. 我对此并不陌生,所以我有这个问题。 Should I use Object serialization or should I save it in XML format? 我应该使用对象序列化还是应将其保存为XML格式? I realize that an XML format has its advantages that's why I am asking (If I want to save it in XML format, I use XMLEncoder class right?) Which one should I use? 我意识到XML格式有其优点,这就是为什么我要问(如果我想将其保存为XML格式,我使用XMLEncoder类对吗?)我应该使用哪一个?

Serialization is much more efficient than XML and easier to get working. 序列化比XML高效得多,并且更易于使用。

Writing out an object in a serialized form is a little faster than text/XML form. 以序列化形式写出对象比文本/ XML形式快一点。 Serialized form is much more compact and so saves memory and bandwidth. 序列化格式更紧凑,因此节省了内存和带宽。 It's also much faster and easier to parse than XML. 它也比XML解析起来更快,更容易。

However you want to use XML if you want to edit these files or use them with other program languages (it's just easier to work with). 但是,如果要编辑这些文件或将它们与其他程序语言一起使用,则要使用XML(使用起来更容易)。

I would go with serialization . 我会进行序列化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM