catch32
catch32

Reputation: 18572

How to customise the xml namespace with JAXB unmarshalling?

I have met some trouble with JAXB unmarshalling xml file to Request object and sent it to servis.

Instead of correct response it returns error - Unknown user.

I'm using next schema:

xml file with test date => parsing to RQ object => sent to servis => take response and check it.

Here is source xml file:

<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>

I'm using JAXB to parse and cast to my RQ object.

It returns strange error instead of responce.

I just write unparsed request to xml file:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<ns1:OTA_AirLowFareSearchRQ xmlns:ns1="http://www.opentravel.org/OTA/2003/05" Version="2.001" EchoToken="50987"
                            SequenceNmbr="1" TimeStamp="2003-11-20T00:44:10.000Z" Target="Production"
                            DirectFlightsOnly="false">
    <ns1:POS>
        <ns1:TPA_Extensions>
            <ns1:TPA_Extension>
                <ns1:PromoRatesRequired Value="false"/>
                <ns1:UserName Value="342561"/>
                <ns1:UserPassword Value="1234"/>
                <ns1:ClearCache Value="true"/>
            </ns1:TPA_Extension>
        </ns1:TPA_Extensions>
    </ns1:POS>
    <ns1:OriginDestinationInformation>
        <ns1:DepartureDateTime>2015-04-13T00:00:00</ns1:DepartureDateTime>
        <ns1:OriginLocation LocationCode="DUB" CodeContext="IATA"/>
        <ns1:DestinationLocation LocationCode="CDG" CodeContext="IATA"/>
    </ns1:OriginDestinationInformation>
    <ns1:TravelPreferences OriginDestinationRPHs="" ETicketDesired="false" SmokingAllowed="false">
        <ns1:CabinPref PreferLevel="Preferred" Cabin="Economy"/>
    </ns1:TravelPreferences>
    <ns1:TravelerInfoSummary>
        <ns1:AirTravelerAvail>
            <ns1:AirTravelerAvail>
                <ns1:PassengerTypeQuantity Code="ADT" Quantity="1"/>
                <ns1:PassengerTypeQuantity Code="CHD" Quantity="0"/>
                <ns1:PassengerTypeQuantity Code="INF" Quantity="1"/>
            </ns1:AirTravelerAvail>
        </ns1:AirTravelerAvail>
    </ns1:TravelerInfoSummary>
    <ns1:dataStatus>SUCCESS_LOW_FARE_SEARCH_REQUEST</ns1:dataStatus>
</ns1:OTA_AirLowFareSearchRQ>

It has a lot pesky ns1: nameprefix.

I want to know how to unmarshal without this redundant prefixes?

Upvotes: 1

Views: 490

Answers (1)

bdoughan
bdoughan

Reputation: 148987

You can properly map the namespace qualification in your XML document using the following package level @XmlSchema annotation. A packaage level annotation is put in a special class called package-info.

package.info.java

Below is the complete source for the package-info class.

@XmlSchema( 
    namespace = "http://www.opentravel.org/OTA/2003/05", 
    elementFormDefault = XmlNsForm.QUALIFIED) 
package example;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

For More Information on JAXB and Namespace

Below are links to posts on my blog that you may find useful:

Upvotes: 1

Related Questions