Link Details

Link 91693 thumbnail
User 214988 avatar

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?
  • 9
  • 1
  • 1748
  • 763

Comments

Add your comment
User 152955 avatar

Gregg Bolinger replied ago:

1 votes Vote down Vote up Reply

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?

User 308895 avatar

jvalentino replied ago:

0 votes Vote down Vote up Reply

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:

 

;<POJO name="me"/;>
;<POJO;><name;<me/;<name;>/;<POJO;>

User 308895 avatar

jvalentino replied ago:

0 votes Vote down Vote up Reply

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.

User 282958 avatar

cowardlydragon replied ago:

0 votes Vote down Vote up Reply

XStream
,

User 283139 avatar

ceaseoleo replied ago:

0 votes Vote down Vote up Reply

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

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.

Voters For This Link (9)



Voters Against This Link (1)