用JAXB递归嵌套子元素

时间:2019-01-10 05:25:02

标签: java xml recursion collections jaxb

我如何nest将一个主题置于任意深度?出于具体示例目的,假设深度为三。

输出:

thufir@dur:~/jaxb$ 
thufir@dur:~/jaxb$ cat books_output.xml 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<library>
    <name>709835212516681240</name>
    <endowments>
        <donor>851702071896450320</donor>
        <books isbn="712995029112128487">
            <authors unique_id="543560880038512055"/>
            <authors unique_id="629967972623061383"/>
            <subjects uniqueID="642983729830052747">
                <subjects unique_id="472945298746057528"/>
                <subjects unique_id="734463657288566454"/>
            </subjects>
        </books>
        <books isbn="657269166726981434">
            <authors unique_id="798044107987766747"/>
            <authors unique_id="043631974912667285"/>
            <subjects uniqueID="366199396221406304">
                <subjects unique_id="891235638870828181"/>
                <subjects unique_id="605075244007086273"/>
            </subjects>
        </books>
        <books isbn="435951238523048362">
            <authors unique_id="115151492495016063"/>
            <authors unique_id="256913007794974921"/>
            <subjects uniqueID="488452902465476765">
                <subjects unique_id="832603865556218638"/>
                <subjects unique_id="604353342365464484"/>
            </subjects>
        </books>
    </endowments>
    <endowments>
        <donor>288499836672612522</donor>
        <books isbn="783904569226316026">
            <authors unique_id="467216680792879591"/>
            <authors unique_id="038686688647255220"/>
            <subjects uniqueID="545515858053057034">
                <subjects unique_id="088606114189908587"/>
                <subjects unique_id="997019693614009285"/>
            </subjects>
        </books>
        <books isbn="660615901337764926">
            <authors unique_id="503438437399266225"/>
            <authors unique_id="804550099915858695"/>
            <subjects uniqueID="546708077973568579">
                <subjects unique_id="104381555493749656"/>
                <subjects unique_id="707529270924972072"/>
            </subjects>
        </books>
        <books isbn="303599198706181965">
            <authors unique_id="614847983245369057"/>
            <authors unique_id="909538176887336486"/>
            <subjects uniqueID="135266654851817926">
                <subjects unique_id="945878682464095815"/>
                <subjects unique_id="955156265551544793"/>
            </subjects>
        </books>
    </endowments>
    <endowments>
        <donor>069716773093574212</donor>
        <books isbn="686043059681127705">
            <authors unique_id="350340754388923966"/>
            <authors unique_id="804623924167328021"/>
            <subjects uniqueID="488453730179351257">
                <subjects unique_id="823555619991551950"/>
                <subjects unique_id="527690117248109909"/>
            </subjects>
        </books>
        <books isbn="289921180116023910">
            <authors unique_id="130745547031824876"/>
            <authors unique_id="756323079037275702"/>
            <subjects uniqueID="120876239371414690">
                <subjects unique_id="090909627322023765"/>
                <subjects unique_id="621045662056501153"/>
            </subjects>
        </books>
        <books isbn="415838161928123087">
            <authors unique_id="911287012901702652"/>
            <authors unique_id="429574603017074903"/>
            <subjects uniqueID="415384742133064719">
                <subjects unique_id="361093208677364558"/>
                <subjects unique_id="263337956957877994"/>
            </subjects>
        </books>
    </endowments>
    <endowments>
        <donor>047726662861179356</donor>
        <books isbn="823427525942077368">
            <authors unique_id="285150460243157210"/>
            <authors unique_id="152954736133811739"/>
            <subjects uniqueID="682264620008926801">
                <subjects unique_id="137076695775834229"/>
                <subjects unique_id="604314155789431114"/>
            </subjects>
        </books>
        <books isbn="433698141619851127">
            <authors unique_id="439685395915661179"/>
            <authors unique_id="020408127657671345"/>
            <subjects uniqueID="720554660718757479">
                <subjects unique_id="170635342721085485"/>
                <subjects unique_id="120260612852043224"/>
            </subjects>
        </books>
        <books isbn="666999518001978729">
            <authors unique_id="070451379799749407"/>
            <authors unique_id="091614597267037086"/>
            <subjects uniqueID="992945289656050064">
                <subjects unique_id="118333065984562769"/>
                <subjects unique_id="717605044083746579"/>
            </subjects>
        </books>
    </endowments>
</library>
thufir@dur:~/jaxb$ 

Book的源代码为:

package net.bounceme.dur.jaxb.hello.world.book;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlType(propOrder = {"isbn", "title", "authors", "subjects"})
@XmlAccessorType(XmlAccessType.FIELD)
public class Book {

    private static final Logger LOG = Logger.getLogger(Book.class.getName());

    @XmlAttribute
    private String isbn;

    @XmlElement
    private String title;

    @XmlElement
    private List<Author> authors = new ArrayList<>();

    @XmlElement
    private Subjects subjects = null;

    private Book() {
    }

    public Book(String isbn) {
        this.isbn = isbn;
    }

    @Override
    public String toString() {
        return getTitle() + getIsbn();
    }

    void add(Author author) {
        getAuthors().add(author);
    }

    public String getIsbn() {
        return isbn;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public List<Author> getAuthors() {
        return authors;
    }

    public void setAuthors(List<Author> authors) {
        this.authors = authors;
    }

    public Subjects getSubjects() {
        return subjects;
    }

    public void setSubjects(Subjects subjects) {
        this.subjects = subjects;
    }

}

Subjects的源代码为:

package net.bounceme.dur.jaxb.hello.world.book;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {"uniqueID", "subjects"})
public class Subjects {

    private static final Logger LOG = Logger.getLogger(Subjects.class.getName());

    @XmlAttribute
    private String uniqueID;

    @XmlElement
    private List<Subject> subjects = new ArrayList<>();

    Subjects(String uniqueID) {
        this.uniqueID = uniqueID;
    }

    public String getUniqueID() {
        return uniqueID;
    }

    public void setUniqueID(String uniqueID) {
        this.uniqueID = uniqueID;
    }

    public List<Subject> getSubjects() {
        return subjects;
    }

    public void setSubjects(List<Subject> subjects) {
        this.subjects = subjects;
    }

    void add(Subject subject) {
        subjects.add(subject);
    }

}

XML上的IBM example

0 个答案:

没有答案