xml转Bean
JAXBContext context = JAXBContext.newInstance(Bean.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Bean bean = (Bean) unmarshaller.unmarshal(new StringReader(xml));
Bean转Xml
JAXBContext context = JAXBContext.newInstance(Bean.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
StringWriter writer = new StringWriter();
marshaller.marshal(bean, writer);
JAXBContext context = JAXBContext.newInstance(Bean.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Bean bean = (Bean) unmarshaller.unmarshal(new StringReader(xml));
Bean转Xml
JAXBContext context = JAXBContext.newInstance(Bean.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
StringWriter writer = new StringWriter();
marshaller.marshal(bean, writer);