简体   繁体   中英

How to create nested Root Element in JAXB when creating xml from Java object using JAXB

**XML to be generated from java object**  
 <automobiles>
    <cars>
        <type></type>
        <car>
            <model></model>
            <maxspeed></maxspeed>
        </car>
        <car>
            <model></model>
            <maxspeed></maxspeed>
        </car>
    </cars>
    <bikes>
        <type></type>
        <bike>
            <model></model>
            <maxspeed></maxspeed>
        </bike>
        <bike>
            <model></model>
            <maxspeed></maxspeed>
        </bike>
    </bikes>
</automobiles>

// the object class to hold values for the xml is

@XmlRootElement(name = "automobiles")
class Automobiles {
    private List<Cars> cars = null;
    private List<Bikes> bikes = null;

    @XmlElement
    public List<Bikes> getBikes() {
        return bikes;
    }

    public void setBikes(List<Bikes> bikes) {
        this.bikes = bikes;
    }

    @XmlElement
    public List<Cars> getCars() {
        return cars;
    }

    public void setCars(List<Cars> cars) {
        this.cars = cars;
    }
}

@XmlRootElement(name = "cars")
class Cars {
    private List<Car> car = null;

    @XmlElement
    public List<Car> getCar() {
        return car;
    }

    public void setCar(List<Car> car) {
        this.car = car;
    }
}

@XmlRootElement(name = "bikes")
class Bikes {
    private List<Bike> bike = null;

    @XmlElement
    public List<Bike> getBikes() {
        return bike;
    }

    public void setBikes(List<Bike> bike) {
        this.bike = bike;
    }
}

@XmlRootElement(name = "Car")
class Car {
    private String model = null;
    private String maxspeed = null;

    @XmlElement
    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    @XmlElement
    public String getMaxspeed() {
        return maxspeed;
    }

    public void setMaxspeed(String maxspeed) {
        this.maxspeed = maxspeed;
    }
}

@XmlRootElement(name = "Bike")
class Bike {
    private String model = null;
    private String maxspeed = null;

    @XmlElement
    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    @XmlElement
    public String getMaxspeed() {
        return maxspeed;
    }

    public void setMaxspeed(String maxspeed) {
        this.maxspeed = maxspeed;
    }
}

Here the above xml is the format I need to generate using JAXB marshaling. I'm new to JAXB. Is the object class and annotation that I have used is correct? can someone please help me out on this.

You didn't say which problem you have with your code. But I observed these things;

You need the @XmlRootElement only on your root element, ie on class Automobiles , but not on the others.

You use the @XmlElement annotation without specifying the name . Then the XML element name is derived from the method name. This is fine in most cases, but not in all cases. For example in class Bikes you have

@XmlElement
public List<Bike> getBikes() { ... }

This is implicitly mapped to the XML element <bikes> , which is wrong, because you want it to be mapped to <bike> . You can fix this in two ways:

Either by specifying the name explicitly in the annotation (the preferred way)

 @XmlElement(name = "bike")
 public List<Bike> getBikes() { ... }

Or by changing the method name (not preferred, because the singular method name is weird then)

 @XmlElement
 public List<Bike> getBike() { ... }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM