有哪些开源库可用于将 XML 转换为 java 值对象?
What kind of open-source libraries are available to convert XML into a java value object?
在 .Net 中,有一种方法可以通过 xml 序列化和属性轻松完成此操作.我想在java中有一些并行.我知道如何使用 DOM 或 SAX 解析器来做到这一点,但我想知道是否有更简单的方法.
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.
我有一个预定义的 XML 格式,看起来像这样.
I have a predefined XML format that looks something like this.
<FOOBAR_DATA>
<ID>12345</ID>
<MESSAGE>Hello World!</MESSAGE>
<DATE>22/04/2009</DATE>
<NAME>Fred</NAME>
</FOOBAR_DATA>
在 .Net 中,我可以做这样的事情来将我的对象绑定到数据.
In .Net, I can do something like this to bind my object to the data.
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()
{
}
}
}
我想知道是否有一种使用注释的方法,类似于 .Net 或 Hibernate,它允许我将值对象绑定到预定义的 XML.
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.
我非常喜欢 XStream,尤其是与 JAXB 相比 - 与 JAXB 不同,XStream 不需要您拥有 XSD.如果您有一些要序列化和反序列化为 XML 的类,那就太好了,而无需创建 XSD、运行 jaxc 以从该模式生成类等繁重的工作.另一方面,XStream 很漂亮轻量级.
I like XStream alot, 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.
(当然,有很多时候 JAXB 是合适的,例如当您有一个适合这种场合的预先存在的 XSD 时......)
(Of course, there are plenty of times where JAXB is appropriate, such as when you have a pre-existing XSD that fits the occasion...)
这篇关于如何将 XML 转换为 java 值对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!