简体   繁体   English

如何使用JAXB解组来自定义xml名称空间?

[英]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. 我在将JAXB解组xml文件到Request对象并将其发送到servis时遇到了一些麻烦。

Instead of correct response it returns error - Unknown user . 它返回错误- Unknown user而不是正确的响应。

I'm using next schema: 我正在使用下一个架构:

xml file with test date => parsing to RQ object => sent to servis => take response and check it. 测试日期=>解析到RQ对象=>发送到servis =>的xml文件获取响应并进行检查。

Here is source xml file: 这是源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>

I'm using JAXB to parse and cast to my RQ object. 我正在使用JAXB解析并投射到我的RQ对象。

It returns strange error instead of responce. 它返回奇怪的错误而不是响应。

I just write unparsed request to xml file: 我只是将未解析的请求写入xml文件:

<?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. 它有很多讨厌的ns1: nameprefix。

I want to know how to unmarshal without this redundant prefixes? 我想知道如何在没有这些多余前缀的情况下解组吗?

You can properly map the namespace qualification in your XML document using the following package level @XmlSchema annotation. 您可以使用以下包级别@XmlSchema批注在XML文档中正确映射名称空间限定。 A packaage level annotation is put in a special class called package-info . 包装级别注释放在称为package-info的特殊类中。

package.info.java package.info.java

Below is the complete source for the package-info class. 以下是package-info类的完整资源。

@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 有关JAXB和命名空间的更多信息

Below are links to posts on my blog that you may find useful: 以下是指向我博客上的帖子的链接,您可能会发现它们有用:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM