XMLEncoder和XStream的相对优势是什么? [英] What are the relative advantages of XMLEncoder and XStream?

查看:424
本文介绍了XMLEncoder和XStream的相对优势是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想在XML中存储许多小配置对象,我不太关心格式。内置于 XMLDecoder 的类JDK可行,据我所知, XStream 以类似的方式工作。

Suppose I want to store many small configuration objects in XML, and I don't care too much about the format. The XMLDecoder class built into the JDK would work, and from what I hear, XStream works in a similar way.

每个库有什么好处?

推荐答案

我真的很喜欢 XStream
库。作为提供的Java对象的结果,它输出相当简单的xml
非常好。它非常适合从xml中再现
对象。而且,我们的第三方库之一
无论如何已经依赖它。

I really like the XStream library. It does a really good job of outputting fairly simple xml as a result of a provided Java object. It works great for reproducing the object back from the xml as well. And, one of our 3rd party libraries already depended on it anyway.


  • 我们选择使用它因为我们想要
    我们的xml是人类可读的。使用
    别名函数可以让它更好

  • We chose to use it because we wanted our xml to be human readable. Using the alias function makes it much nicer.

如果
想要某些部分,你可以扩展库
的对象以更好的方式反序列化。我们
在一个案例中这样做,所以文件
将有一套度数,
分钟,以及纬度
和经度的秒数,而不是两个
双打。

You can extend the library if you want some portion of an object to deserialize in a nicer fashion. We did this in one case so the file would have a set of degrees, minutes, and seconds for a latitude and longitude, instead of two doubles.

两分钟的教程总结了基本用法,但保留信息的
利息在一个地方,我会尝试在这里加上
,只是缩短一点。

The two minute tutorial sums up the basic usage, but in the interest of keeping the information in one spot, I'll try to sum it up here, just a little shorter.

// define your classes
public class Person {
  private String firstname;
  private PhoneNumber phone;
  // ... constructors and methods
}

public class PhoneNumber {
  private int code;
  private String number;
  // ... constructors and methods
}

然后使用库写出xml。

Then use the library for write out the xml.

// initial the libray
XStream xstream = new XStream();
xstream.alias("person", Person.class); // elementName, Class
xstream.alias("phone", PhoneNumber.class); 

// make your objects
Person joe = new Person("Joe");
joe.setPhone(new PhoneNumber(123, "1234-456"));

// convert xml
String xml = xstream.toXML(joe);

您的输出将如下所示:

You output will look like this:

<person>
  <firstname>Joe</firstname>
  <phone>
    <code>123</code>
    <number>1234-456</number>
  </phone>
</person>

返回:

Person newJoe = (Person)xstream.fromXML(xml);

XMLEncoder是为Java bean序列化提供的。我最后一次使用它,
文件看起来相当讨厌。如果真的不关心文件是什么样的,那么
可以为你工作,你可以避免第三方依赖,这也很好。我希望使序列化更漂亮的可能性对XMLEncoder来说也是一个挑战。

The XMLEncoder is provided for Java bean serialization. The last time I used it, the file looked fairly nasty. If really don't care what the file looks like, it could work for you and you get to avoid a 3rd party dependency, which is also nice. I'd expect the possibility of making the serialization prettier would be more a challenge with the XMLEncoder as well.

如果你没有别名,XStream会输出完整的类名名称。如果上面的Person类有

XStream outputs the full class name if you don't alias the name. If the Person class above had

package example;

xml将具有example.Person而不仅仅是person 。

the xml would have "example.Person" instead of just "person".

这篇关于XMLEncoder和XStream的相对优势是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆