简单的Java Xml到POJO映射/绑定? [英] Simple Java Xml to POJO mapping/binding?

查看:491
本文介绍了简单的Java Xml到POJO映射/绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图找出将xml文件映射到普通旧java对象的最简单方法。

I'm trying to figure out the simplest way to map an xml file to to a plain old java object.

注意:在我的例子中,xml与我想要的POJO不完全匹配。

Note: That in my example the xml doesn't quite match up with my intended POJO.

///////// THE XML
<?xml version="1.0" encoding="UTF-8"?>
<Animal>
  <standardName>
    <Name>Cat</Name>
  </standardName>
  <standardVersion>
    <VersionIdentifier>V02.00</VersionIdentifier>
  </standardVersion>
</Animal>


////// THE INTENDED POJO
class Animal
{
 private String name;
 private String versionIdentifier;
}

常规JAXB(带注释)不能用作JAXM元素名称注释不允许我指定嵌套元素。 (即标准名称/名称)。

Regular JAXB (with annotations) won't work as the JAXM Element name annotations don't allow me to specifiy nested elements. (i.e. standardName/Name).

我看过Jibx,但看起来过于复杂,并没有为我想做的事情提供完整的例子。

I've looked at Jibx but it seems overly complicated, and no full examples are provided for what I want to do.

卡斯特罗似乎能够做我想做的事情(使用映射文件),但我想知道是否还有其他可能的解决方案。 (可能这会让我跳过映射文件,只允许我在注释中指定所有内容)。

Castro seems like it would be able to do what I want (using mapping files), but I wonder if there are any other possible solutions. (Possibly that would allow me to skip mapping files, and just allow me to specify everything in annotations).

谢谢

推荐答案

这篇文章可以帮到你......只需要你知道xpath
http://onjava.com/onjava/2007/09/07/schema-less-java-xml-data-binding-with-vtd-xml.html

This article may help you... it only requires you to know xpath http://onjava.com/onjava/2007/09/07/schema-less-java-xml-data-binding-with-vtd-xml.html

这篇关于简单的Java Xml到POJO映射/绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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