public class Person { private String name; private int age; } <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <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> public class Main { public static void main(String[] args) { Person person = new Person(); person.setName("John"); person.setAge(30); IBindingFactory factory = BindingDirectory.getFactory(Person.class); IMarshallingContext context = factory.createMarshallingContext(); StringWriter writer = new StringWriter(); context.setOutput(writer); context.marshalDocument(person); String xml = writer.toString(); System.out.println(xml); IUnmarshallingContext unmarshallingContext = factory.createUnmarshallingContext(); Person person2 = (Person) unmarshallingContext.unmarshalDocument( new StringReader(xml), null); System.out.println(person2.getName()); System.out.println(person2.getAge()); } }


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