@XmlRootElement public class Person { private String name; private int age; } <bindings version="1.0" xmlns="http://java.sun.com/xml/ns/jaxb"> <bindings> <package name="com.example"> <class name="Person"> <property name="name" /> <property name="age" /> </class> </package> </bindings> </bindings> JAXBContext context = JAXBContext.newInstance(Person.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(person, System.out); File xmlFile = new File("person.xml"); JAXBContext context = JAXBContext.newInstance(Person.class); Unmarshaller unmarshaller = context.createUnmarshaller(); Person person = (Person) unmarshaller.unmarshal(xmlFile);


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