如何将 XML 转换为 java 值对象?
有哪些开源库可以将 XML 转换为 java 值对象?
在 .Net 中,有一种方法可以通过 xml 序列化和属性轻松地做到这一点。 我想象java中有一些并行的东西。 我知道如何使用 DOM 或 SAX 解析器来做到这一点,但我想知道是否有更简单的方法。
我有一个预定义的 XML 格式,看起来像这样。
<FOOBAR_DATA>
<ID>12345</ID>
<MESSAGE>Hello World!</MESSAGE>
<DATE>22/04/2009</DATE>
<NAME>Fred</NAME>
</FOOBAR_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()
{
}
}
}
我想知道是否有一种使用注释的方法(类似于 .Net 或 Hibernate),它允许我将值对象绑定到预定义的 XML。
What kind of open-source libraries are available to convert XML into a java value object?
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.
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>
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()
{
}
}
}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我非常喜欢 XStream,尤其是与 JAXB 相比 - 与 JAXB 不同,XStream 不需要您拥有 XSD 。 如果您有一些想要序列化和反序列化为 XML 的类,而不需要创建 XSD、运行 jaxc 从该模式生成类等,那么这是很棒的。另一方面,XStream 非常漂亮轻的。
(当然,有很多时候 JAXB 是合适的,例如当您有一个适合该场合的预先存在的 XSD 时......)
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.
(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 允许您将 XML 模式 (XSD) 文件转换为 Java 类的集合。 这可能比 Andy(顺便说一句,非常棒)答案提供的
XMLEncoder
/Serialized
方法更加“结构化”。JAXB allows you to convert an XML Schema (XSD) file into a collection of Java classes. This may be more "structured" than the
XMLEncoder
/Serializable
approach that Andy's (excellent, by the way) answer provides.Java 有一个 XMLEncoder 您也许可以用于将对象序列化为 XML。 唯一的要求是您的对象实现“可序列化”。
这是一个示例:
http://www.developer.com/java/web/文章.php/1377961
Java has an XMLEncoder that you might be able to use to serialize an object to XML. The only requirement is that your object implements "Serializable."
Here's an example:
http://www.developer.com/java/web/article.php/1377961
JiBX 是另一种选择。
有关 Java 到 XML 数据绑定的更多意见,请参阅 Java 中的 XML 序列化?
JiBX is another option.
For more opinions on Java-to-XML data binding, see XML serialization in Java?