Simple Java Xml to POJO mapping/binding?
Asked Answered
C

4

11

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

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;
}

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

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

Thanks

Cytoplasm answered 30/10, 2009 at 19:47 Comment(1)
As an additional note, the mapping/binding would have to work both ways.Cytoplasm
D
3

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

Delk answered 30/10, 2009 at 19:53 Comment(2)
Seems like this will probably we the best choice, (not having mapping files, or annotations), but just having some form of translator methods using xpath, and possibly vtdxml, or dom.Cytoplasm
be careful with digestor, it is not a full parser and can be very slow for large XmL docsDelk
E
5

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:

Ey answered 7/7, 2010 at 16:27 Comment(2)
EclipseLink JAXB (MOXy) link redirects to 404Neddra
here is a working Link: eclipse.org/eclipselinkBirchard
D
3

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

Delk answered 30/10, 2009 at 19:53 Comment(2)
Seems like this will probably we the best choice, (not having mapping files, or annotations), but just having some form of translator methods using xpath, and possibly vtdxml, or dom.Cytoplasm
be careful with digestor, it is not a full parser and can be very slow for large XmL docsDelk
W
3

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.

Witchcraft answered 30/10, 2009 at 19:54 Comment(0)
F
2

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
Faradic answered 30/10, 2009 at 19:52 Comment(2)
I read the tutorial, but unless I missed something .. there seems to be a couple of extra steps required (compared to Castro), i.e. the requirements for running bindgen. (Which isn't really explained too easily on the website). It seems like an overcomplication having to run bindgen on your mapping files before you can use them. Are there are simple tutorials/snippets available? i.e. given this xml file, this xml mapping file, and this chunk of code, you will get this POJO instance.Cytoplasm
EclipseLink JAXB (MOXy) also offers a mapping file. This gives you the freedom to use annotations (standard JAXB ones plus extensions) or a mapping file.Ey

© 2022 - 2024 — McMap. All rights reserved.