<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Employee"> <xs:complexType> <xs:sequence> <xs:element name="firstName" type="xs:string"/> <xs:element name="lastName" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> xjc employee.xsd import javax.xml.bind.annotation.*; @XmlRootElement(name = "Employee") @XmlAccessorType(XmlAccessType.FIELD) public class Employee { @XmlElement private String firstName; @XmlElement private String lastName; // Getters and setters } import javax.xml.bind.*; public class Main { public static void main(String[] args) throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class); Marshaller marshaller = jaxbContext.createMarshaller(); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); Employee employee = new Employee(); employee.setFirstName("John"); employee.setLastName("Doe"); marshaller.marshal(employee, System.out); Employee parsedEmployee = (Employee) unmarshaller.unmarshal(new File("employee.xml")); } }


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