如何将 XML 转换为 Java 值对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/779755/
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 19:24:13  来源:igfitidea点击:

How do I convert XML into a java value object?

javaxmlxml-parsing

提问by Kevin

What kind of open-source libraries are available to convert XML into a java value object?

有哪些开源库可以将 XML 转换为 java 值对象?

In .Net, there is a way to easily do this with xml serialization and attributes. I would imagine there some parallel in java. I know how to do this with a DOM or SAX parser, but I was wondering if there was an easier way.

在 .Net 中,有一种方法可以使用 xml 序列化和属性轻松地做到这一点。我会想象在java中有一些并行。我知道如何使用 DOM 或 SAX 解析器执行此操作,但我想知道是否有更简单的方法。

I have a predefined XML format that looks something like this.

我有一个看起来像这样的预定义 XML 格式。

<FOOBAR_DATA>
  <ID>12345</ID>
  <MESSAGE>Hello World!</MESSAGE>
  <DATE>22/04/2009</DATE>
  <NAME>Fred</NAME>
</FOOBAR_DATA>

In .Net, I can do something like this to bind my object to the data.

在 .Net 中,我可以做这样的事情来将我的对象绑定到数据。

using System;
using System.Xml.Serialization;

    namespace FooBarData.Serialization
    {
      [XmlRoot("FOOBAR_DATA")]
      public class FooBarData
  {
    private int _ID = 0;
    [XmlElement("ID")]
    public int ID
    {
      get { return this._ID; }
      set { this._ID = value; }
    }

    private string _Message = "";
    [XmlElement("MESSAGE")]
    public string Message
    {
      get { return this._Message; }
      set { this._Message = value; }
    }

    private string _Name = "";
    [XmlElement("NAME")]
    public string Name
    {
      get { return this._Name; }
      set { this._Name = value; }
    }

    private Date _Date;
    [XmlElement("DATE")]
    public Date Date
    {
      get { return this._Date; }
      set { this._Date= value; }
    }

    public FooBarData()
    {
    }
  }
}

I was wondering if there was a method using annotations, similar to .Net or perhaps Hibernate, that will allow me to bind my value object to the predefined-XML.

我想知道是否有一种使用注释的方法,类似于 .Net 或 Hibernate,它允许我将我的值对象绑定到预定义的 XML。

采纳答案by matt b

I like XStreamalot, especially compared to JAXB - unlike JAXB, XStream doesn't need you to have an XSD. It's great if you have a handful of classes you want to serialize and deserialize to XML, without the heavy-handed-ness of needing to create a XSD, run jaxc to generate classes from that schema, etc. XStream on the other hand is pretty lightweight.

我非常喜欢XStream,尤其是与 JAXB 相比 - 与 JAXB 不同,XStream 不需要您拥有 XSD。如果您有一些要序列化和反序列化为 XML 的类,那就太好了,而无需创建 XSD、运行 jaxc 以从该模式生成类等的繁重工作。 另一方面,XStream 非常漂亮轻的。

(Of course, there are plenty of times where JAXB is appropriate, such as when you have a pre-existing XSD that fits the occasion...)

(当然,有很多时候 JAXB 是合适的,比如当你有一个适合这种场合的预先存在的 XSD 时......)

回答by Andy White

Java has an XMLEncoderthat you might be able to use to serialize an object to XML. The only requirement is that your object implements "Serializable."

Java 有一个XMLEncoder,您可以使用它来将对象序列化为 XML。唯一的要求是您的对象实现“可序列化”。

Here's an example:

下面是一个例子:

http://www.developer.com/java/web/article.php/1377961

http://www.developer.com/java/web/article.php/1377961

回答by Chris Jester-Young

JAXBallows you to convert an XML Schema (XSD) file into a collection of Java classes. This may be more "structured" than the XMLEncoder/Serializableapproach that Andy's (excellent, by the way) answer provides.

JAXB允许您将 XML 模式 (XSD) 文件转换为 Java 类的集合。这可能比安迪(顺便说一句,很棒)的答案提供的XMLEncoder/Serializable方法更“结构化” 。

回答by Cheeso

JiBXis another option.

JiBX是另一种选择。

For more opinions on Java-to-XML data binding, see XML serialization in Java?

有关 Java 到 XML 数据绑定的更多意见,请参阅Java 中的 XML 序列化?