Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JAXB: Access to private fields by the framework

0 views
Skip to first unread message

Stanimir Stamenkov

unread,
Jun 30, 2008, 5:14:08 AM6/30/08
to
How does the JAXB framework realize access to private fields? I've
tried the following example works just fine in both directions:
marshaling and unmarshaling.

---TuBean.java
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="DOC")
public class TuBean {

@XmlElement(name="FOO")
private String foo;

@XmlElement(name="BAR")
private String bar;

@XmlElement(name="BAZ")
private String baz;

@XmlElement(name="TAZ")
private String taz;

public static void main(String[] args) throws Exception {
TuBean data = new TuBean();
data.foo = "123";
data.bar = "xyz";
data.taz = "***";

JAXBContext bindContext =
JAXBContext.newInstance(TuBean.class);
Marshaller marshaller = bindContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT,
Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
Boolean.TRUE);

StringWriter buf = new StringWriter();
marshaller.marshal(data, buf);
marshaller.marshal(data, System.out);
System.out.println();

Unmarshaller unmarshaller =
bindContext.createUnmarshaller();
data = (TuBean) unmarshaller
.unmarshal(new StringReader(buf.toString()));
System.out.println("foo=" + data.foo);
System.out.println("bar=" + data.bar);
System.out.println("baz=" + data.baz);
System.out.println("taz=" + data.taz);
}

}
---TuBean.java--

--
Stanimir

0 new messages