2014-09-19 32 views
0

我想將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

如何解決這個問題?

+0

根元素是'OTA_AirLowFareSearchRQ',我沒有看到任何映射到該類的類?這是需要傳遞給jaxbcontext的類,與其他人作爲屬性 – maress 2014-09-19 14:56:17

+0

@maress我有這個類,它是unmarshalled好的。 – 2014-09-19 18:17:16

回答

0

首先,我討厭JAXB!

這就是說,我字面上有一類中的每個元素中指定

@XmlElemnt(命名空間= 「命名空間」)

。 JAXB根本不會將默認命名空間分配給當前的xml dom。

默認情況下,JAXB似乎與ElementFormDefault="unqualified"AttributeFormDefault="qualified"(我可能會混淆這一點)一起工作,但是這使得它解開根元素,只考慮屬於默認命名空間的屬性。

如果命名空間有一個前綴,例如xmlns:ns="namespace",然後JAXB將正確地編組屬於這個命名空間的元素。

如果您有基於XML的控制,只是有一個前綴,默認的命名空間,就像

的xmlns:NS = 「http://www.opentravel.org/OTA/2003/05」

如果你不,確保有

@XmlElement(namespace = "http://www.opentravel.org/OTA/2003/05") 

每次不是一個屬性元素。

package com.anosym.stackoverflow.stackoverflow; 

import java.util.Calendar; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 

import static com.anosym.stackoverflow.stackoverflow.Main.calendarString; 
import static javax.xml.bind.annotation.XmlAccessType.FIELD; 

@XmlRootElement(name = "OTA_AirLowFareSearchRQ", namespace = "http://www.opentravel.org/OTA/2003/05") 
@XmlAccessorType(FIELD) 
public class OTAAirLowFareSearchRQ { 

    @XmlAttribute(name = "EchoToken") 
    private String echoToken; 
    @XmlAttribute(name = "SequenceNmbr ") 
    private int sequenceNmbr; 
    @XmlAttribute(name = "Target") 
    private String target; 
    @XmlAttribute(name = "TimeStamp") 
    private Calendar timeStamp; 
    @XmlAttribute(name = "Version") 
    private String version; 
    @XmlElement(name = "POS", namespace = "http://www.opentravel.org/OTA/2003/05") 
    private POS pos; 
    @XmlElement(name = "OriginDestinationInformation", namespace = "http://www.opentravel.org/OTA/2003/05") 
    private OriginDestinationInformation destinationInformation; 

    public OTAAirLowFareSearchRQ(String echoToken, int sequenceNmbr, String target, Calendar timeStamp, String version, 
           POS pos, OriginDestinationInformation destinationInformation) { 
     this.echoToken = echoToken; 
     this.sequenceNmbr = sequenceNmbr; 
     this.target = target; 
     this.timeStamp = timeStamp; 
     this.version = version; 
     this.pos = pos; 
     this.destinationInformation = destinationInformation; 
    } 

    public OTAAirLowFareSearchRQ() { 
    } 

    @Override 
    public String toString() { 
     return "OTAAirLowFareSearchRQ{" 
       + "echoToken=" + echoToken + ", " 
       + "sequenceNmbr=" + sequenceNmbr + ", " 
       + "target=" + target + ", " 
       + "timeStamp=" + calendarString(timeStamp) + ", " 
       + "version=" + version + ", " 
       + "pos=" + pos + ", " 
       + "destinationInformation=" + destinationInformation + '}'; 
    } 

} 

package com.anosym.stackoverflow.stackoverflow; 

import java.util.List; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlElementWrapper; 

import static javax.xml.bind.annotation.XmlAccessType.FIELD; 

@XmlAccessorType(FIELD) 
public class POS { 

    @XmlElement(name = "TPA_Extension", namespace = "http://www.opentravel.org/OTA/2003/05") 
    @XmlElementWrapper(name = "TPA_Extensions", namespace = "http://www.opentravel.org/OTA/2003/05") 
    private List<TPAExtension> tpaExtensions; 

    public POS(List<TPAExtension> tpaExtensions) { 
     this.tpaExtensions = tpaExtensions; 
    } 

    public POS() { 
    } 

    @Override 
    public String toString() { 
     return "POS{" + "tpaExtensions=" + tpaExtensions + '}'; 
    } 

} 
+0

我將命名空間添加到xml文件。沒有它不能'xmlns =「http://www.opentravel.org/OTA/2003/05」'。但是你看到了它得到的輸出。我無法理解爲什麼會發生這種情況。你能給出一些建議如何解析**從XML到沒有JAXB **的對象?解析這個'request.xml'會更好一些。 – 2014-09-20 09:21:15