Java中的XML序列化?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:26:58  来源:igfitidea点击:

XML serialization in Java?

javaxmlserialization

提问by Dmitry Shechtman

What is the Java analogue of .NET's XML serialization?

.NET 的 XML 序列化的 Java 模拟是什么?

采纳答案by Cheekysoft

2008 AnswerThe "Official" Java API for this is now JAXB - Java API for XML Binding. See Tutorial by Oracle. The reference implementation lives at http://jaxb.java.net/

2008 年回答“官方”Java API 现在是 JAXB - Java API for XML Binding。请参阅Oracle 教程。参考实现位于http://jaxb.java.net/

2018 UpdateNote that the Java EE and CORBA Modules are deprecated in SE in JDK9 and to be removed from SE in JDK11. Therefore, to use JAXB it will either need to be in your existing enterprise class environment bundled by your e.g. app server, or you will need to bring it in manually.

2018 年更新请注意,Java EE 和 CORBA 模块在 JDK9 的 SE 中已弃用,并将从 JDK11 的 SE 中删除。因此,要使用 JAXB,它要么需要在由您的应用程序服务器捆绑的现有企业级环境中,要么您需要手动将其引入。

回答by Theo

If you're talking about automatic XML serialization of objects, check out Castor:

如果您正在谈论对象的自动 XML 序列化,请查看Castor

Castor is an Open Source data binding framework for Java[tm]. It's the shortest path between Java objects, XML documents and relational tables. Castor provides Java-to-XML binding, Java-to-SQL persistence, and more.

Castor 是一个用于 Java[tm] 的开源数据绑定框架。它是 Java 对象、XML 文档和关系表之间的最短路径。Castor 提供 Java-to-XML 绑定、Java-to-SQL 持久性等。

回答by Barak Schiller

XStreamis pretty good at serializing object to XML without much configuration and money! (it's under BSD license).

XStream非常擅长将对象序列化为 XML,无需太多配置和金钱!(它在 BSD 许可下)。

We used it in one of our project to replace the plain old java-serialization and it worked almost out of the box.

我们在我们的一个项目中使用它来替换普通的旧 java 序列化,它几乎开箱即用。

回答by John Meagher

XMLBeansworks great if you have a schema for your XML. It creates Java objects for the schema and creates easy to use parse methods.

如果您的 XML 有一个架构,那么XMLBeans工作得很好。它为模式创建 Java 对象并创建易于使用的解析方法。

回答by Bartosz Bierkowski

Usually I use jaxbor XMLBeansif I need to create objects serializable to XML. Now, I can see that XStreammight be very useful as it's nonintrusive and has really simple api. I'll play with it soon and probably use it. The only drawback I noticed is that I can't create object's id on my own for cross referencing.

如果我需要创建可序列化为 XML 的对象,通常我会使用jaxbXMLBeans。现在,我可以看到XStream可能非常有用,因为它是非侵入式的并且具有非常简单的 api。我很快就会玩它,可能会使用它。我注意到的唯一缺点是我无法自己创建对象的 id 以进行交叉引用。

@Barak Schiller
Thanks for posting link to XStream!

@Barak Schiller
感谢您发布 XStream 链接!

回答by ARKBAN

"Simple XML Serialization" Project

“简单 XML 序列化”项目

You may want to look at the Simple XML Serializationproject. It is the closest thing I've found to the System.Xml.Serialization in .Net.

您可能想查看Simple XML Serialization项目。这是我在 .Net 中发现的最接近 System.Xml.Serialization 的东西。

回答by Cheeso

Don't forget JiBX.

不要忘记JiBX

回答by so_mv

JAXB is part of JDK standard edition version 1.6+. So it is FREEand no extra libraries to download and manage. A simple example can be found here

JAXB 是 JDK 标准版 1.6+ 版的一部分。因此FREE,无需下载和管理额外的库。一个简单的例子可以在这里找到

XStream seems to be dead. Last update was on Dec 6 2008. Simpleseems as easy and simpler as JAXB but I could not find any licensing information to evaluate it for enterprise use.

XStream 似乎已经死了。上次更新是在 2008 年 12 月 6 日。 Simple看起来和 JAXB 一样容易和简单,但我找不到任何许可信息来评估它是否适合企业使用。

回答by m.genova

if you want a structured solution(like ORM) then JAXB2 is a good solution.

如果你想要一个结构化的解决方案(比如 ORM),那么 JAXB2 是一个很好的解决方案。

If you want a serialization like DOT NET then you could use Long Term Persistence of JavaBeans Components

如果你想要像 DOT NET 这样的序列化,那么你可以使用JavaBeans 组件的长期持久性

The choice depends on use of serialization.

选择取决于序列化的使用。

回答by Mishax

Worth mentioning that since version 1.4, Java had the classes java.beans.XMLEncoder and java.beans.XMLDecoder. These classes perform XML encoding which is at least very comparable to XML Serialization and in some circumstances might do the trick for you.

值得一提的是,从 1.4 版开始,Java 就有了 java.beans.XMLEncoder 和 java.beans.XMLDecoder 类。这些类执行 XML 编码,这至少与 XML 序列化非常相似,并且在某些情况下可能会为您提供帮助。

If your class sticks to the JavaBeans specification for its getters and setters, this method is straightforward to use and you don't need a schema. With the following caveats:

如果您的类的 getter 和 setter 遵循 JavaBeans 规范,则此方法使用起来很简单,并且您不需要模式。有以下注意事项:

  • As with normal Java serialization
    • coding and decoding run over a InputStream and OutputStream
    • the process uses the familar writeObject and readObject methods
  • In contrast to normal Java serialization
    • the encoding but also decoding causes constructors and initializers to be invoked
    • encoding and decoding work regardless if your class implements Serializable or not
    • transient modifiers are not taken into account
    • works only for public classes, that have public constructors
  • 与普通的 Java 序列化一样
    • 编码和解码在 InputStream 和 OutputStream 上运行
    • 该过程使用熟悉的 writeObject 和 readObject 方法
  • 与普通的 Java 序列化相反
    • 编码和解码都会导致构造函数和初始值设定项被调用
    • 无论您的类是否实现 Serializable 编码和解码工作
    • 不考虑瞬态修饰符
    • 仅适用于具有公共构造函数的公共类

For example, take the following declaration:

例如,采用以下声明:

public class NPair {
  public NPair() { }
  int number1 = 0;
  int number2 = 0;
  public void setNumber1(int value) { number1 = value;}
  public int getNumber1() { return number1; }
  public void setNumber2(int value) { number2 = value; }
  public int getNumber2() {return number2;}
}

Executing this code:

执行这段代码:

NPair fe = new NPair();
fe.setNumber1(12);
fe.setNumber2(13);
FileOutputStream fos1 = new FileOutputStream("d:\ser.xml");
java.beans.XMLEncoder xe1 = new java.beans.XMLEncoder(fos1);
xe1.writeObject(fe);
xe1.close();

Would result in the following file:

将导致以下文件:

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.7.0_02" class="java.beans.XMLDecoder">
 <object class="NPair">
  <void property="number1">
   <int>12</int>
  </void>
  <void property="number2">
   <int>13</int>
  </void>
 </object>
</java>