2
我想以一個有序的xml編組我的對象。 我的jaxb類如下所示: - 我已經使用Xmlordering,但它不工作來訂購我的XML。JAXB 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>
有人能指導我在哪裏,我錯了? 謝謝。
謝謝布萊斯。不知怎的,我幾分鐘前解決了它。 也會發布我的答案。 – NamingException