@XmlRootElement作用及用法
阿新 • • 發佈:2019-02-05
package com.yanek.test; import java.util.ArrayList; import java.util.List; import com.yanek.test.JaxbUtil.CollectionWrapper; public class Test { /** * @param args */ public static void main(String[] args) { //建立java物件 Hotel hotel=new Hotel(); hotel.setId(1); hotel.setName("name1"); RoomTypeVO t1=new RoomTypeVO(); t1.setPrice("20"); t1.setTypeid(1); t1.setTypename("typename1"); RoomTypeVO t2=new RoomTypeVO(); t2.setPrice("30"); t2.setTypeid(2); t2.setTypename("typename2"); List<RoomTypeVO> RoomTypeVOs=new ArrayList<RoomTypeVO>(); RoomTypeVOs.add(t1); RoomTypeVOs.add(t2); hotel.setRoomTypeVOs(RoomTypeVOs); //將java物件轉換為XML字串 JaxbUtil requestBinder = new JaxbUtil(Hotel.class, CollectionWrapper.class); String retXml = requestBinder.toXml(hotel, "utf-8"); System.out.println("xml:"+retXml); //將xml字串轉換為java物件 JaxbUtil resultBinder = new JaxbUtil(Hotel.class, CollectionWrapper.class); Hotel hotelObj = resultBinder.fromXml(retXml); System.out.println("hotelid:"+hotelObj.getId()); System.out.println("hotelname:"+hotelObj.getName()); for(RoomTypeVO roomTypeVO:hotelObj.getRoomTypeVOs()) { System.out.println("Typeid:"+roomTypeVO.getTypeid()); System.out.println("Typename:"+roomTypeVO.getTypename()); } } }