简单的 Java Xml 到 POJO 映射/绑定?

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

Simple Java Xml to POJO mapping/binding?

javaxmljibx

提问by vicsz

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

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

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

注意:在我的示例中,xml 与我想要的 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;
}

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).

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

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

我看过 Jibx,但它似乎过于复杂,并且没有提供我想要做什么的完整示例。

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).

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

Thanks

谢谢

采纳答案by vtd-xml-author

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

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

回答by bdoughan

EclipseLink JAXB (MOXy)allows you to do the path based mapping that you are looking for:

EclipseLink JAXB (MOXy)允许您执行您正在寻找的基于路径的映射:

@XmlRootElement 
class Animal 
{ 
 @XmlPath("standardName/Name/text()")
 private String name; 

 @XmlPath("standardVersion/VersionIdentifier/text()");
 private String versionIdentifier; 
} 

For more information see:

有关更多信息,请参阅:

EclipseLink also allows the metadata to be specified using an external configuration file:

EclipseLink 还允许使用外部配置文件指定元数据:

回答by kdgregory

Jakarta Commons Digestershould do what you want.

Jakarta Commons Digester应该做你想做的。

Alternatively, I would recommend writing a transformation class that uses XPath to retrieve elements from the XML.

或者,我建议编写一个使用 XPath 从 XML 检索元素的转换类。

回答by SingleShot

I consider JiBX the best of the bunch (JAXB, Castor, XMLBeans, etc.), particularly because I favor mapping files over annotations. Admittedly it has a decent learning curve, but the website has a lot of good examples. You must have missed the tutorial.

我认为 JiBX 是同类产品中最好的(JAXB、Castor、XMLBeans 等),特别是因为我更喜欢映射文件而不是注释。不可否认,它有一个不错的学习曲线,但该网站有很多很好的例子。你一定错过了教程

If you are only going one way (XML --> POJO) you could use Digester.

如果您只采用一种方式(XML --> POJO),您可以使用Digester

Side comment: I prefer mapping files over annotations because annotations:

旁注:我更喜欢映射文件而不是注释,因为注释:

  • clutter the code (especially when using annotations from several products)
  • mix concerns (XML, database, etc. in domain layer)
  • can only bind to a single XML (or database, or web service, etc.) representation
  • 使代码混乱(尤其是在使用来自多个产品的注释时)
  • 混合关注点(域层中的 XML、数据库等)
  • 只能绑定到单个 XML(或数据库或 Web 服务等)表示