<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> \t<xs:element name="student" type="studentType"/> \t<xs:complexType name="studentType"> \t\t<xs:sequence> \t\t\t<xs:element name="name" type="xs:string"/> \t\t\t<xs:element name="age" type="xs:int"/> \t\t\t<xs:element name="grade" type="xs:string"/> \t\t</xs:sequence> \t\t<xs:attribute name="id" type="xs:string"/> \t</xs:complexType> </xs:schema> xjc -d outputDir schemaFile.xsd import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import java.io.File; public class XMLParser { public static void main(String[] args) { try { File file = new File("student.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(Student.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); Student student = (Student) jaxbUnmarshaller.unmarshal(file); System.out.println("Student ID: " + student.getId()); System.out.println("Student Name: " + student.getName()); System.out.println("Student Age: " + student.getAge()); System.out.println("Student Grade: " + student.getGrade()); } catch (JAXBException e) { e.printStackTrace(); } } }


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