JAXB again

the relevant stuff is mainly in javax.xml.bind package. include by default in JDK6, versions vary. tools like schemagen are not, so need to be downloaded from jaxb site.

basic example:

@XmlRootElement(name = "TimedValue")
@XmlType(propOrder = {"value", "time"})
public class TimeValue {
  private String value = null;
  private Date time = null;
  @XmlElement(name = "value")  public String getValue() {
    return value;
  }
  @XmlElement(name = "time")  public Date getTime() {
    return time;
  }
}

The result is something like

<TimedValue>
  <value>hello</value>
  <time>2008-09-29T04:49:45</time>
</TimedValue>

Note that without the “propOrder” setting the ordering of elements is random due to unpredictable reflection.

Note, I never tried to compile the above, lol, it is just an example. The given names are also a match for the class information but could be named in any other way as long as the way is XML compliant. I prefer explicit naming for understanding and evolution.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s