Potrzebujesz pomocy w formatowaniu wyjścia JAXB

Mam kilka obiektów, powiedzmy dwa, A i B. Te obiekty z tej samej klasy. Muszę zebrać te obiekty za pomocą JAXB, a wyjściowy XML powinien być w tej formie:

<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>

Jak wygenerować ten format w JAXB? Każda pomoc jest doceniana.

Aktualizacja: Mówiąc dokładniej, załóżmy, że mamy taką klasę ludzką:

<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>

a naszą główną klasą jest:

<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>

Wtedy wyjście będzie:

<code><?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<human>
    <age>24</age>
    <name>John</name>
    <nationalID>123456789</nationalID>
</human>
</code>

Teraz potrzebuję danych wyjściowych, aby wyglądały tak:

<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>

To pomoże mi narysować drzewo obiektów XML bez atrybutów (tylko przez ID), a następnie wszystkie definicje poniżej drzewa. Czy jest to możliwe przy użyciu JAXB lub jakiejkolwiek innej implementacji?

questionAnswers(2)

yourAnswerToTheQuestion