0
**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>
從Java對象創建XML時,創建嵌套的根元素//對象類保存值的XML是如何使用JAXB JAXB
@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;
}
}
這裏上面的XML是我需要的格式使用JAXB編組生成。 我是JAXB的新手。我使用的對象類和註釋是否正確?有人可以幫我解決這個問題。
我覺得你的課堂設計出現了錯誤的軌道。你的'汽車'包含一個'List','Cars'包含一個'List '。 如果'Automobiles'只包含一個'List ',那就更自然了。 –