¿Necesitas ayuda para formatear la salida JAXB?
Tengo algunos objetos, digamos dos, A y B. Estos objetos de la misma clase. Necesito reunir estos objetos utilizando JAXB y el XML de salida debería estar en esta forma:
<code><Root> <A> <ID> an id </ID> </A> <B> <ID> an id </ID> </B> </Root> <!-- Then all A and B attributes must be listed !--> <A> <ID> an id </ID> <attribute1> value </attribute1> <attribute2> value </attribute2> </A> <B> <ID> an id </ID> <attribute1> value </attribute1> <attribute2> value </attribute2> </B> </code>
¿Cómo generar este formato en JAXB? Cualquier ayuda es apreciada.
Actualizar: Para ser más específicos, supongamos que tenemos clase humana como esta:
<code>@XmlRootElement public class Human { private String name; private int age; private Integer nationalID; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Integer getNationalID() { return nationalID; } public void setNationalID(Integer nationalID) { this.nationalID = nationalID; } } </code>
y nuestra clase principal es:
<code>public class Main3 { public static void main(String[] args) throws JAXBException { Human human1 = new Human(); human1.setName("John"); human1.setAge(24); human1.setNationalID(Integer.valueOf(123456789)); JAXBContext context = JAXBContext.newInstance(Human.class); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); StringWriter stringWriter = new StringWriter(); m.marshal(human1, stringWriter); System.out.println(stringWriter.toString()); } } </code>
Entonces la salida será:
<code><?xml version="1.0" encoding="UTF-8" standalone="yes"?> <human> <age>24</age> <name>John</name> <nationalID>123456789</nationalID> </human> </code>
Ahora necesito que la salida sea así:
<code><?xml version="1.0" encoding="UTF-8" standalone="yes"?> <human> <nationalID>123456789</nationalID> </human> <human> <nationalID>123456789</nationalID> <age>24</age> <name>John</name> </human> </code>
Y esto me ayudará a dibujar un árbol de objetos XML sin los atributos (solo por ID) y luego todas las definiciones debajo del árbol. ¿Es esto posible utilizando JAXB o cualquier otra implementación?