gpt4 book ai didi

java - JAXB xml 排序或序列或属性排序

转载 作者:行者123 更新时间:2023-11-30 11:19:27 26 4
gpt4 key购买 nike

我想在有序的 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>

有人可以指导我哪里错了吗?谢谢。

最佳答案

我没有在外部类上使用 propOrder,而是在内部类上使用它,而且它的效果很好。

@XmlType(propOrder={"agentID","password","opt","info","dateFrom","dateTo","rateConvert","compressReply","returnWarnings","config"})
public static class OptionInfoRequest {
}

关于java - JAXB xml 排序或序列或属性排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23218236/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com