<dependency> <groupId>org.jibx</groupId> <artifactId>jibx-bind</artifactId> <version>{version}</version> </dependency> public class Person { private String name; private int age; } <binding> <mapping name="Person" class="com.example.Person"> <structure> <value name="name" field="name"/> <value name="age" field="age" usage="optional"/> </structure> </mapping> </binding> IBindingFactory bfact = BindingDirectory.getFactory(Person.class); IMarshallingContext mctx = bfact.createMarshallingContext(); IUnmarshallingContext uctx = bfact.createUnmarshallingContext(); Person person = new Person("John Doe", 25); StringWriter writer = new StringWriter(); mctx.setOutput(writer); mctx.marshalDocument(person); String xml = writer.toString(); System.out.println(xml); StringReader reader = new StringReader(xml); uctx.setInput(reader); Person person2 = (Person) uctx.unmarshalDocument(); System.out.println(person2.getName()); System.out.println(person2.getAge());


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