public class Person { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import java.io.File; public class JAXBExample { public static void main(String[] args) { Person person = new Person(); person.setAge(25); try { File file = new File("person.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(Person.class); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(person, file); marshaller.marshal(person, System.out); } catch (JAXBException e) { e.printStackTrace(); } } } import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import java.io.File; public class JAXBExample { public static void main(String[] args) { try { File file = new File("person.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(Person.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); Person person = (Person) unmarshaller.unmarshal(file); } catch (JAXBException e) { e.printStackTrace(); } } }


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