Simple Java Xml to POJO mapping/binding?

谁都会走 提交于 2019-11-28 10:05:50

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

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

@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:

Jakarta Commons Digester should do what you want.

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

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.

If you are only going one way (XML --> POJO) you could use 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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!