我想将xml文件的上下文转换为java对象。不能从XML文件中取消与JAXB的元素
但是有些部分被提取好了,有些部分有空值。
下面是XML文件:
<OTA_AirLowFareSearchRQ EchoToken="50987" SequenceNmbr="1" Target="Production" TimeStamp="2003-11-19T19:44:10-05:00"
Version="2.001"
xsi:schemaLocation="http://www.opentravel.org/OTA/2003/05 OTA_AirLowFareSearchRQ.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.opentravel.org/OTA/2003/05">
<POS>
<TPA_Extensions>
<TPA_Extension>
<PromoRatesRequired Value="false"/>
<UserName Value="342561"/>
<UserPassword Value="1234"/>
<ClearCache Value="true"/>
</TPA_Extension>
</TPA_Extensions>
</POS>
<OriginDestinationInformation>
<DepartureDateTime>2015-04-13T00:00:00</DepartureDateTime>
<OriginLocation LocationCode="DUB"/>
<DestinationLocation LocationCode="CDG"/>
</OriginDestinationInformation>
<TravelPreferences>
<CabinPref PreferLevel="Preferred" Cabin="Economy"/>
</TravelPreferences>
<TravelerInfoSummary>
<AirTravelerAvail>
<PassengerTypeQuantity Code="ADT" Quantity="1"/>
<PassengerTypeQuantity Code="CHD" Quantity="0"/>
<PassengerTypeQuantity Code="INF" Quantity="1"/>
</AirTravelerAvail>
</TravelerInfoSummary>
</OTA_AirLowFareSearchRQ>
我有提取 'OriginDestinationInformation' 和 'AirTravelerAvail' 的麻烦。
这里是的main():
public static void main(String[] args) {
try {
JAXBContext context = JAXBContext.newInstance(OTAAirLowFareSearchRQ.class);
Unmarshaller um = context.createUnmarshaller();
OTAAirLowFareSearchRQ rq = (OTAAirLowFareSearchRQ) um.unmarshal(new FileReader(FILE_NAME));
System.out.println(rq);
} catch (FileNotFoundException | JAXBException e) {
LOGGER.error(e);
}
}
下面是输出片段:
originDestinationInformation=[OriginDestinationInformation{
departureDateTime=null,
originLocation=null,
destinationLocation=null,
alternateLocationInfo=null,
rph='null',
refNumber=null}],
travelerInfoSummary=TravelerInfoSummary{
ticketingCountryCode='null',
specificPTCIndicator=null,
airTravelerAvails=null},
echoToken='50987',
timeStamp=2003-11-19T19:44:10-05:00,
target='Production',
version=2.001,
transactionIdentifier='null',
sequenceNmbr=1,
还有更多有趣的部分OriginDestinationInformation
:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "OriginDestinationInformation", propOrder = {
"departureDateTime",
"originLocation",
"destinationLocation",
"tpaExtensions",
"alternateLocationInfo",
"rph",
"refNumber"
})
public static class OriginDestinationInformation extends OriginDestinationInformationType {
@XmlElement(name = "DepartureDateTime", required = true)
protected TimeInstantType departureDateTime;
@XmlElement(name = "OriginLocation", required = true)
protected OriginLocation originLocation;
@XmlElement(name = "DestinationLocation", required = true)
protected DestinationLocation destinationLocation;
@Transient
@XmlElement(name = "AlternateLocationInfo")
protected AlternateLocationInfo alternateLocationInfo;
@XmlElement(name = "TPA_Extensions")
protected TPAExtensionsType tpaExtensions;
@XmlAttribute(name = "RPH")
protected String rph;
@XmlAttribute(name = "RefNumber")
protected Integer refNumber;
// getters and setters
TimeInstantType
:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "TimeInstantType", propOrder = {
"value"
})
public class TimeInstantType {
@Property("dateTime")
@XmlValue
protected String value;
// rest of class
OriginLocation
:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "OriginLocation")
public static class OriginLocation extends LocationType {
@XmlAttribute(name = "MultiAirportCityInd")
protected Boolean multiAirportCityInd;
@XmlAttribute(name = "AlternateLocationInd")
protected Boolean alternateLocationInd;
@XmlAttribute(name = "LocationCode")
protected String locationCode;
DestinationLocation
:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "DestinationLocation")
public static class DestinationLocation extends LocationType {
@XmlAttribute(name = "MultiAirportCityInd")
protected Boolean multiAirportCityInd;
@XmlAttribute(name = "AlternateLocationInd")
protected Boolean alternateLocationInd;
@XmlAttribute(name = "LocationCode")
protected String locationCode;
和对象列表TravelerInfoSummary
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "TravelerInfoSummary", propOrder = {
"airTravelerAvails",
"ticketingCountryCode",
"specificPTCIndicator"
})
public static class TravelerInfoSummary extends TravelerInfoSummaryType {
@Embedded
@XmlElement(name = "AirTravelerAvail")
protected List<AirTravelerAvail> airTravelerAvails;
AirTravelerAvail
:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "AirTravelerAvail")
public class AirTravelerAvail {
@Embedded
@XmlElement(name = "PassengerTypeQuantity")
@XmlElementWrapper(name = "AirTravelerAvail")
protected List<PassengerTypeQuantity> passengerTypeQuantities;
我无法弄清楚什么是错在这里。我试图
@XmlElementWrapper(NAME = “AirTravelerAvail”)
为对象的列表中,但不断收到null
。
如何解决这个问题?
根元素是'OTA_AirLowFareSearchRQ',我没有看到任何映射到该类的类?这是需要传递给jaxbcontext的类,与其他人作为属性 – maress 2014-09-19 14:56:17
@maress我有这个类,它是unmarshalled好的。 – 2014-09-19 18:17:16