javaben转xml
pos:
@Data
public class Pos {
@JSONField(name = "Source")
private Source source;
@Data
public static class Source{
@JSONField(name = "ISOCountry")
private String isoCountry = "CN";
@JSONField(name = "ISOCurrency")
private String isoCurrency = "CNY";
@JSONField(name = "PseudoCityCode")
private String pseudoCityCode = "3P";
@JSONField(name = "Channel")
private String channel = "B2B";
@JSONField(name = "RequestorID")
private RequestorID requestorID;
@Data
public static class RequestorID{
@JSONField(name = "ID")
private String id = "密码";
@JSONField(name = "CompanyName")
private CompanyName companyName;
@Data
public static class CompanyName{
@JSONField(name = "Code")
private String code="xx公司";
@JSONField(name = "CompanyShortName")
private String companyShortName="SD";
}
}
}
public static Pos ofDefault() {
Pos pos = new Pos();
Source source = new Source();
Source.RequestorID requestorID = new Source.RequestorID();
Source.RequestorID.CompanyName companyName = new Source.RequestorID.CompanyName();
requestorID.setCompanyName(companyName);
source.setRequestorID(requestorID);
pos.setSource(source);
return pos;
}
}
AirLowFareSearchRQ:
@Data
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
@XmlType(name = "OTA_AirLowFareSearchRQ", propOrder = {})
public class AirLowFareSearchRQ {
@JSONField(name = "POS")
@XmlElement
protected Pos pos ;
@JSONField(name = "OriginDestinationInformation")
@XmlElement
protected OriginDestinationInformation originDestinationInformation;
@JSONField(name = "SpecificFlightInfo")
@XmlElement
protected SpecificFlightInfo specificFlightInfo ;
@JSONField(name = "TravelPreferences")
@XmlElement
protected TravelPreferences travelPreferences;
@JSONField(name = "TravelerInfoSummary")
@XmlElement
protected TravelerInfoSummary travelerInfoSummary ;
@JSONField(name = "xmlns")
@XmlAttribute
protected String xmlns;
@JSONField(name = "MaxResponses")
@XmlAttribute
protected String maxResponses;
@JSONField(name = "TransactionIdentifier")
@XmlAttribute
protected String transactionIdentifier;
@JSONField(name = "Version")
@XmlAttribute
protected String version;
@JSONField(name = "PrimaryLangID")
@XmlAttribute
protected String primaryLangID;
@Data
public static class OriginDestinationInformation{
@JSONField(name = "DepartureDateTime")
private String departureDateTime;
@JSONField(name = "OriginLocation")
private Location originLocation;
@JSONField(name = "DestinationLocation")
private Location destinationLocation;
@Data
public static class Location{
@JSONField(name = "CodeContext")
private String codeContext;
@JSONField(name = "LocationCode")
private String locationCode;
}
}
@Data
public static class SpecificFlightInfo{
@JSONField(name = "BookingClassPref")
private BookingClassPref bookingClassPref = new BookingClassPref();
@Data
public static class BookingClassPref{
@JSONField(name = "ResBookDesigCode")
private String besBookDesigCode;
}
}
@Data
public static class TravelPreferences{
@JSONField(name = "ETicketDesired")
private String eTicketDesired;
}
@Data
public static class TravelerInfoSummary{
@JSONField(name = "AirTravelerAvail")
private AirTravelerAvail airTravelerAvail;
@Data
public static class AirTravelerAvail{
@JSONField(name = "PassengerTypeQuantity")
private List<PassengerTypeQuantity> passengerTypeQuantity;
@Data
public static class PassengerTypeQuantity{
@JSONField(name = "Code")
private String code;
@JSONField(name = "Quantity")
private String quantity;
}
}
}
public static AirLowFareSearchRQ ofDefault() {
AirLowFareSearchRQ airLowFareSearchRQ = new AirLowFareSearchRQ();
airLowFareSearchRQ.setXmlns("http://www.opentravel.org/OTA/2003/05");
airLowFareSearchRQ.setMaxResponses("30");
airLowFareSearchRQ.setTransactionIdentifier("0");
airLowFareSearchRQ.setVersion("2.005");
//
OriginDestinationInformation information = new OriginDestinationInformation();
information.setDestinationLocation(new OriginDestinationInformation.Location());
information.setOriginLocation(new OriginDestinationInformation.Location());
airLowFareSearchRQ.setOriginDestinationInformation(information);
//
SpecificFlightInfo specificFlightInfo = new SpecificFlightInfo();
SpecificFlightInfo.BookingClassPref pref = new SpecificFlightInfo.BookingClassPref();
pref.setBesBookDesigCode("Y");
specificFlightInfo.setBookingClassPref(pref);
airLowFareSearchRQ.setSpecificFlightInfo(specificFlightInfo);
//
TravelPreferences preferences = new TravelPreferences();
preferences.setETicketDesired("true");
airLowFareSearchRQ.setTravelPreferences(preferences);
//pos
airLowFareSearchRQ.setPos(Pos.ofDefault());
return airLowFareSearchRQ;
}
}
utils工具类
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;
/**
* @date: 2018/04/13
* @author: wangcd
* @version: 1.0.0
* @description:Jaxb2工具类.
**/
public class JaxbUtil {
/**
* 以UTF-8编码的格式将JavaBean转成XML
* @param o
* @return
*/
public static String convertToXml(Object o){
return convertToXml(o,"UTF-8");
}
/**
* 将JavaBean转成XML
* @param obj JavaBean对象
* @param encoding xml编码格式,例,"UTF-8"
* @return
*/
public static String convertToXml(Object obj,String encoding){
String result = "";
try {
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
StringWriter writer = new StringWriter();
marshaller.marshal(obj, writer);
result = writer.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 将XML转成JavaBean
* @param xml xml字符串
* @param cls JavaBean的class名称 例,Object.class
* @param <T>
* @return 返回转换后的JavaBean
*/
public static <T> T convertToJavaBean(String xml,Class<T> cls){
T t = null;
try {
JAXBContext jaxbContext = JAXBContext.newInstance(cls);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
t = (T) unmarshaller.unmarshal(new StringReader(xml));
} catch (JAXBException e) {
e.printStackTrace();
}
return t;
}
}
测试:
@Test
void contextLoads() {
AirLowFareSearchRQ searchRQ = AirLowFareSearchRQ.ofDefault();
searchRQ.setPrimaryLangID("zh");
searchRQ.getOriginDestinationInformation().setDepartureDateTime("2020-10-10");
searchRQ.getOriginDestinationInformation().getOriginLocation().setCodeContext("IATA");
searchRQ.getOriginDestinationInformation().getOriginLocation().setLocationCode("TNA");
searchRQ.getOriginDestinationInformation().getDestinationLocation().setLocationCode("IATA");
searchRQ.getOriginDestinationInformation().getDestinationLocation().setLocationCode("XMN");
AirLowFareSearchRQ.TravelerInfoSummary travelerInfoSummary = new AirLowFareSearchRQ.TravelerInfoSummary();
AirLowFareSearchRQ.TravelerInfoSummary.AirTravelerAvail airTravelerAvail = new AirLowFareSearchRQ.TravelerInfoSummary.AirTravelerAvail();
List<AirLowFareSearchRQ.TravelerInfoSummary.AirTravelerAvail.PassengerTypeQuantity> passengerTypeQuantityList = new ArrayList<>();
AirLowFareSearchRQ.TravelerInfoSummary.AirTravelerAvail.PassengerTypeQuantity passengerTypeQuantity = new AirLowFareSearchRQ.TravelerInfoSummary.AirTravelerAvail.PassengerTypeQuantity();
passengerTypeQuantity.setCode("ADT");
passengerTypeQuantity.setQuantity("1");
passengerTypeQuantityList.add(passengerTypeQuantity);
airTravelerAvail.setPassengerTypeQuantity(passengerTypeQuantityList);
travelerInfoSummary.setAirTravelerAvail(airTravelerAvail);
searchRQ.setTravelerInfoSummary(travelerInfoSummary);
String str = JaxbUtil.convertToXml(searchRQ);
System.out.println(str);
}
测试结果:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<airLowFareSearchRQ xmlns="http://www.opentravel.org/OTA/2003/05" maxResponses="30" transactionIdentifier="0" version="2.005" primaryLangID="zh">
<pos>
<source>
<channel>B2B</channel>
<isoCountry>CN</isoCountry>
<isoCurrency>CNY</isoCurrency>
<pseudoCityCode>3P</pseudoCityCode>
<requestorID>
<companyName>
<code>xx公司</code>
<companyShortName>SD</companyShortName>
</companyName>
<id>密码</id>
</requestorID>
</source>
</pos>
<originDestinationInformation>
<departureDateTime>2020-10-10</departureDateTime>
<destinationLocation>
<locationCode>XMN</locationCode>
</destinationLocation>
<originLocation>
<codeContext>IATA</codeContext>
<locationCode>TNA</locationCode>
</originLocation>
</originDestinationInformation>
<specificFlightInfo>
<bookingClassPref>
<besBookDesigCode>Y</besBookDesigCode>
</bookingClassPref>
</specificFlightInfo>
<travelPreferences>
<ETicketDesired>true</ETicketDesired>
</travelPreferences>
<travelerInfoSummary>
<airTravelerAvail>
<passengerTypeQuantity>
<code>ADT</code>
<quantity>1</quantity>
</passengerTypeQuantity>
</airTravelerAvail>
</travelerInfoSummary>
</airLowFareSearchRQ>