import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; @XmlRootElement public class Person { @XmlElement private String name; @XmlElement private int age; } public class Main { public static void main(String[] args) throws Exception { JAXBContext context = JAXBContext.newInstance(Person.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); Unmarshaller unmarshaller = context.createUnmarshaller(); Person person = new Person(); person.setName("John"); person.setAge(30); marshaller.marshal(person, System.out); String xml = "<Person><name>John</name><age>30</age></Person>"; Person convertedPerson = (Person) unmarshaller.unmarshal(new StringReader(xml)); } }


上一篇:
下一篇:
切换中文