我想在有序的xml中封送我的对象。 我的jaxb课程如下: - 我使用过Xmlordering但它无法订购我的xml。
package mypackage;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import mypackage.TourplanAvailabilityRequest.OptionInfoRequest.RoomConfig;
import mypackage.TourplanAvailabilityRequest.OptionInfoRequest.RoomConfigs;
import com.eos.hotels.data.HotelSearchQuery;
@XmlRootElement(name = "Request")
@XmlType(propOrder={"request"})
public class TourplanAvailabilityRequest {
private OptionInfoRequest request;
public TourplanAvailabilityRequest() {
// TODO Auto-generated constructor stub
}
public TourplanAvailabilityRequest(HotelSearchQuery hsq) {
this(hsq, "null");
}
public TourplanAvailabilityRequest(HotelSearchQuery hsq, String optInfo) {
OptionInfoRequest req = new OptionInfoRequest();
req.setAgentID("AATRAV");
req.setPassword("AATRAV");
req.setOpt("citycode???????");
req.setInfo("GS");
// req.setDateFrom(Calendar.getInstance().setTime(hsq.startDate));
// req.setDateTo(Calendar.getInstance().setTime(hsq.endDate) );
req.setRateConvert("N");
req.setReturnWarnings("N");
req.setCompressReply("N");
RoomConfigs roomsToBook = new RoomConfigs();
List<RoomConfig> rooms = new ArrayList<RoomConfig>();
for (int i = 0; i < hsq.rooms.length; i++) {
RoomConfig room = new RoomConfig();
room.setAdults(hsq.rooms[i].adults.length);
room.setChildren(hsq.rooms[i].children.length);
room.setRoomType(hsq.m_roomType);
rooms.add(room);
}
roomsToBook.setRoom(rooms);
req.setConfig(roomsToBook);
this.request = req;
}
@XmlElement(name = "OptionInfoRequest")
public OptionInfoRequest getRequest() {
return request;
}
public void setRequest(OptionInfoRequest request) {
this.request = request;
}
public static class OptionInfoRequest {
private String agentID;
private String password;
private String opt;
private String info;
private Calendar dateFrom;
private Calendar dateTo;
private String rateConvert;
private String compressReply;
private String returnWarnings;
private RoomConfigs config;
@XmlElement(name = "AgentID")
public String getAgentID() {
return agentID;
}
public void setAgentID(String agentID) {
this.agentID = agentID;
}
@XmlElement(name = "Password")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@XmlElement(name = "Opt")
public String getOpt() {
return opt;
}
public void setOpt(String opt) {
this.opt = opt;
}
@XmlElement(name = "Info")
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
@XmlElement(name = "DateFrom")
public Calendar getDateFrom() {
return dateFrom;
}
public void setDateFrom(Calendar dateFrom) {
this.dateFrom = dateFrom;
}
@XmlElement(name = "DateTo")
public Calendar getDateTo() {
return dateTo;
}
public void setDateTo(Calendar dateTo) {
this.dateTo = dateTo;
}
@XmlElement(name = "RateConvert")
public String getRateConvert() {
return rateConvert;
}
public void setRateConvert(String rateConvert) {
this.rateConvert = rateConvert;
}
@XmlElement(name = "CompressReply")
public String getCompressReply() {
return compressReply;
}
public void setCompressReply(String compressReply) {
this.compressReply = compressReply;
}
@XmlElement(name = "ReturnWarnings")
public String getReturnWarnings() {
return returnWarnings;
}
public void setReturnWarnings(String returnWarnings) {
this.returnWarnings = returnWarnings;
}
@XmlElement(name = "RoomConfigs")
public RoomConfigs getConfig() {
return config;
}
public void setConfig(RoomConfigs config) {
this.config = config;
}
public static class RoomConfigs {
private List<RoomConfig> room;
@XmlElement(name = "RoomConfig")
public List<RoomConfig> getRoom() {
return room;
}
public void setRoom(List<RoomConfig> room) {
this.room = room;
}
}
public static class RoomConfig {
private int adults;
private int children;
private String roomType;
@XmlElement(name = "Adults")
public int getAdults() {
return adults;
}
public void setAdults(int adults) {
this.adults = adults;
}
@XmlElement(name = "Children")
public int getChildren() {
return children;
}
public void setChildren(int children) {
this.children = children;
}
@XmlElement(name = "RoomType")
public String getRoomType() {
return roomType;
}
public void setRoomType(String roomType) {
this.roomType = roomType;
}
}
}
}
我希望我的xml输出如下所示: -
<?xml version="1.0"?>
<!DOCTYPE Request SYSTEM "hostConnect_3_00_000.dtd">
<Request>
<OptionInfoRequest>
<AgentID>AATRAV</AgentID>
<Password>AATRAV</Password>
<Opt>BKKACAMARIA??????</Opt>
<Info>GS</Info>
<RateConvert>Y</RateConvert>
<RoomConfigs>
<RoomConfig>
<Adults>1</Adults>
<Children>0</Children>
</RoomConfig>
<RoomConfig>
<Adults>1</Adults>
<Children>0</Children>
</RoomConfig>
</RoomConfigs>
</OptionInfoRequest>
</Request>
我当前的xml是: -
<?xml version="1.0" encoding="Unicode" standalone="yes"?>
<Request>
<OptionInfoRequest>
<AgentID>AATRAV</AgentID>
<CompressReply>N</CompressReply>
<RoomConfigs>
<RoomConfig>
<Adults>1</Adults>
<Children>0</Children>
</RoomConfig>
<RoomConfig>
<Adults>1</Adults>
<Children>0</Children>
</RoomConfig>
</RoomConfigs>
<Info>GS</Info>
<Opt>citycode???????</Opt>
<Password>AATRAV</Password>
<RateConvert>N</RateConvert>
<ReturnWarnings>N</ReturnWarnings>
</OptionInfoRequest>
</Request>
有人可以指导我错在哪里吗?
感谢。
答案 0 :(得分:1)
我没有在外层使用propOrder,而是在内部类中使用它,&amp;它就像魅力一样。
@XmlType(propOrder={"agentID","password","opt","info","dateFrom","dateTo","rateConvert","compressReply","returnWarnings","config"})
public static class OptionInfoRequest {
}
答案 1 :(得分:0)
您只需要在propOrder
注释上使用@XmlType
属性来控制每个要控制排序的类。