About Hierarchy at JAXB.marshal

Asked 2 years ago, Updated 2 years ago, 92 views

Hello
We are currently generating xml with the following source code:

public class Main {

    public static void main(String[]args) {

        Kusakusa = new Kusa();
        kusa.setKusa("Gusa");
        Kusa2kusa2 = new Kusa2();
        kusa2.setKusa2("Kusa2");
        kusa.setKusa2(kusa2);
        Kusa3kusa3 = new Kusa3();
        kusa3.setKusa3("Kusa3");
        kusa.setKusa3(kusa3);
        System.out.println("*****************************");
        JAXB.marshal (kusa, System.out);
        System.out.println("*****************************");
    }
}
public class Kusa{

    private String kusa;
    private Kusa2kusa2;
    private Kusa3kusa3;
    @XmlElement(nillable=true)
    public String getKusa(){
        return kusa;
    }

    public void setKusa(Stringkusa){
        This.kusa=kusa;
    }

    public Kusa2getKusa2(){
        return kusa2;
    }

    public void setKusa2(Kusa2kusa2){
        this.kusa2 = kusa2;
    }
    public Kusa3getKusa3(){
        return kusa3;
    }

    public void setKusa3(Kusa3kusa3){
        this.kusa3 = kusa3;
    }

}
public class Kusa2{
    private String kusa2;


    public String getKusa2(){
        return kusa2;
    }
    @XmlElement(nillable=true)
    public void setKusa2(Stringkusa2){
        this.kusa2 = kusa2;
    }

}
public class Kusa3 {
    private String kusa3;

    public String getKusa3(){
        return kusa3;
    }
    @XmlElement(nillable=true)
    public void setKusa3(Stringkusa3){
        this.kusa3 = kusa3;
    }
}

The xml generated by this source is

<?xml version="1.0" encoding="UTF-8" standardone="yes"?>
<kusa>
    <kusa> grass </kusa>
    <kusa2>
        <kusa2> grass2</kusa2>
    </kusa2>
    <kusa3>
        <kusa3> grass3</kusa3>
    </kusa3>
</kusa>

However, what I want to do is divide the classes of kusa2 and kusa3 and

<?xml version="1.0" encoding="UTF-8" standardone="yes"?>
<kusa>
    <kusa> grass </kusa>
    <kusa2>
        <kusa2> grass2</kusa2>
        <kusa3> grass3</kusa3>
    </kusa2>
</kusa>

I would like to print xml as shown in .

I have just started studying, so I may be saying something wrong, but I appreciate your cooperation.

java xml

2022-09-30 11:16

1 Answers

I think I should define a new class with kusa2 and kusa3 in the element.
The class Kusa2Parent is defined below.

class Kusa {

    private String kusa;
    private Kusa2 Parent kusa2 = new Kusa2 Parent();

    @XmlElement(nillable=true)
    public String getKusa(){
        return kusa;
    }

    public void setKusa(Stringkusa){
        This.kusa=kusa;
    }

    @XmlElement(nillable=true, name="kusa2")
    public Kusa2 Parent getKusa2(){
        return kusa2;
    }

    public void setKusa2(Kusa2kusa2){
        this.kusa2.setKusa2(kusa2);
    }

    public void setKusa3(Kusa3kusa3){
        this.kusa2.setKusa3(kusa3);
    }
}

class Kusa2Parent {
    private Kusa2kusa2;
    private Kusa3kusa3;

    @XmlElement
    public String getKusa2(){
        return kusa2.getKusa2();
    }

    @XmlElement
    public String getKusa3(){
        return kusa3.getKusa3();
    }

    public void setKusa2(Kusa2kusa2){
        this.kusa2 = kusa2;
    }

    public void setKusa3(Kusa3kusa3){
        this.kusa3 = kusa3;
    }
}

Code difference


2022-09-30 11:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.