Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Beans > JAXB: Access to...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 1 Topic 1362 of 1403
Post > Topic >>

JAXB: Access to private fields by the framework

by Stanimir Stamenkov <s7an10@[EMAIL PROTECTED] > Jun 30, 2008 at 12:14 PM

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
im****t java.io.StringReader;
im****t java.io.StringWriter;

im****t javax.xml.bind.JAXBContext;
im****t javax.xml.bind.Marshaller;
im****t javax.xml.bind.Unmarshaller;
im****t javax.xml.bind.annotation.XmlElement;
im****t javax.xml.bind.annotation.XmlRootElement;

@[EMAIL PROTECTED]
(name="DOC")
public class TuBean {

     @[EMAIL PROTECTED]
(name="FOO")
     private String foo;

     @[EMAIL PROTECTED]
(name="BAR")
     private String bar;

     @[EMAIL PROTECTED]
(name="BAZ")
     private String baz;

     @[EMAIL PROTECTED]
(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
 




 1 Posts in Topic:
JAXB: Access to private fields by the framework
Stanimir Stamenkov <s7  2008-06-30 12:14:08 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Dec 4 0:42:36 CST 2008.