1. JDK 1.5
2. JWSDP 2.0
3. The jars needed:
%jwsdp2.0 installation dir%/jaxb/lib/jaxb-api.jar
%jwsdp2.0 installation dir%/jaxb/lib/jaxb-impl.jar
%jwsdp2.0 installation dir%/sjsxp/lib/jsr173_api.jar
%jwsdp2.0 installation dir%/jwsdp-shared/lib/activation.jar
4. The using of JAXB2.0 is a little different with before version :
public class Main {
public static void main(String[] args){
try {
JAXBContext jc = JAXBContext.newInstance( "test.po" );
Unmarshaller u = jc.createUnmarshaller();
Foo foo = (Foo)((JAXBElement)u.unmarshal(new File( "test.xml" ))).getValue();
//If no getValue method, will throw an exception of casting class
foo.setAge(28);
ObjectFactory factory = new ObjectFactory();
JAXBElement<Foo> je = factory.createRoot(foo);
/**
*If use foo directorily, will throw an exception for missing an @XmlRootElement annotation
*/
Marshaller m = jc.createMarshaller();
try {
m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(je, new FileOutputStream(new File("test1.xml")));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("foo : "+foo.getAge());
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}