By puredanger
via jvalentino.blogspot.com
Published: Jul 02 2008 / 05:00
I have an XML document and I want to use that document to populate a corresponding set of Java objects. This is a commonly encountered scenario when working with Java, so what is the easiest method for Java XML Binding that requires the least amount of code?
Comments
Gregg Bolinger replied ago:
This is a bad use of annotations. The annotation values could be inferred from the names of the class/properties. Also, as was said, XStream already does this. An extra line of code or two is not a big deal. Why are developers so scared of writing code?
jvalentino replied ago:
The annotation values cannot be inferred if the XML node names are different from the Java class field names, which is why annotations can optionally be used on fields that map to XML nodes; there has to be some way to map the values. Also there is no way using the name alone to tell when going from Java class to XML whether a particular field should be mapped as an attribute of a node or a node of a node. In a POJO with a field called name, the resulting XML could be either of the following:
jvalentino replied ago:
Before doing this I knew that something similar probably already existed, I just was unable to find it. It was also a fun exercise for myself in SAX and Reflection. I also knew that I would get several recommendations if I wrote about it.
cowardlydragon replied ago:
XStream
,
ceaseoleo replied ago:
@Bolinger
jvalentino is correct, if they differ, annotations would be useful in a lot of situations. I am not familiar with Xstream, the article doesn't point to a comparison between these two libraries. Would like to see a comparison between these two offerings.
Voters For This Link (9)
Voters Against This Link (1)