JAXB 2.0 environment

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();

}

}

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值