context = JAXBContext.newInstance(Bean.class
);
//下面代码演示将对象转变为xml
Marshaller mar = context.createMarshaller();
writer = new
StringWriter();
mar.marshal(bean, writer);
//下面代码演示将上面生成的xml转换为对象
reader = new
StringReader(writer.toString());
Unmarshaller unmar = context.createUnmarshaller();
bean = (Bean)unmar.unmarshal(reader);