<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://example.com" targetNamespace="http://example.com" elementFormDefault="qualified"> <xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="age" type="xs:int"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> shell xjc -d src -p com.example.schema schema.xsd import com.example.schema.Person; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import java.io.File; public class Main { public static void main(String[] args) { try { File file = new File("person.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(Person.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); Person person = (Person) jaxbUnmarshaller.unmarshal(file); System.out.println(person.getName()); System.out.println(person.getAge()); } catch (JAXBException e) { e.printStackTrace(); } } }


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