<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="person" type="personType"/> <xs:complexType name="personType"> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="age" type="xs:int"/> </xs:sequence> </xs:complexType> </xs:schema> bash xjc -d src -p com.example.model schema.xsd package com.example; import com.example.model.Person; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import java.io.File; public class Main { public static void main(String[] args) throws Exception { Person person = new Person(); person.setName("Alice"); person.setAge(25); JAXBContext context = JAXBContext.newInstance(Person.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(person, new File("person.xml")); } } <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <person> <name>Alice</name> <age>25</age> </person>


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