JAXB Namespace on each xml tag

2019-03-04 02:19发布

Im using jaxb to generate XML Request. Below are all the code details. I have also tried the package-info thing, but it didn't worked for me. Like i want to add the namespace on each object XML tag.

Hope you guys got my point. Am i doing something wrong or something missing? or is this kind of thing is possible in JAXB Java.

Required Output

<ns0:CustomizedBundlesSubscriptionPortalResultMsg xmlns:ns0="http://www.herman.com/schemas/SubscriptionCustomizedBundles.xsd">
 <ns1:ResultHeader xmlns:ns1="http://www.herman.pk/eil/common_service/types/common_types/v1">
    <ns1:RequestID>1</ns1:RequestID>
    <ns1:Timestamp>20180518160833</ns1:Timestamp>
 </ns1:ResultHeader>
 <ns0:CustomizedBundlesResponseMessage>
    <ns0:AcctChgRec>
       <ns1:AccountType xmlns:ns1="http://www.herman.com/bme/cbsinterface/common">2000</ns1:AccountType>
       <ns1:BalanceId xmlns:ns1="http://www.herman.com/bme/cbsinterface/common">999000000016059109</ns1:BalanceId>
    </ns0:AcctChgRec>
 </ns0:CustomizedBundlesResponseMessage>
</ns0:CustomizedBundlesSubscriptionPortalResultMsg>

Output Im Getting

<ns0:CustomizedBundlesSubscriptionPortalResultMsg xmlns:ns1="http://www.herman.pk/eil/common_service/types/common_types/v1" xmlns:ns0="http://www.herman.com/schemas/SubscriptionCustomizedBundles.xsd" xmlns:ns3="http://www.herman.com/bme/cbsinterface/common">
    <ns1:ResultHeader>
        <ns1:RequestID>1</ns1:RequestID>
        <ns1:Timestamp>20180518160833</ns1:Timestamp>
    </ns1:ResultHeader>
    <ns0:CustomizedBundlesResponseMessage>
        <ns0:AcctChgRec>
            <ns3:AccountType>2000</ns3:AccountType>
            <ns3:BalanceId>999000000016059109</ns3:BalanceId>
        </ns0:AcctChgRec>
    </ns0:CustomizedBundlesResponseMessage>
</ns0:CustomizedBundlesSubscriptionPortalResultMsg>

Below is my JAXB Parent Java Object Class

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "resultHeader",
    "customizedBundlesResponseMessage"
})
@XmlRootElement(name = "CustomizedBundlesSubscriptionPortalResultMsg")
public class CustomizedBundlesSubscriptionPortalResultMsg {

    @XmlElement(name = "ResultHeader", namespace = "http://www.herman.pk/eil/common_service/types/common_types/v1")
    protected ResultHeaderType resultHeader;
    @XmlElement(name = "CustomizedBundlesResponseMessage")
    protected CustomizedBundlesResponseMessage customizedBundlesResponseMessage;
}

Below is my inner JAXB Object Java Class

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "acctChgRec"
})
@XmlRootElement(name = "CustomizedBundlesResponseMessage")
public class CustomizedBundlesResponseMessage {

    @XmlElement(name = "AcctChgRec")
    protected List<AcctChgRecType> acctChgRec;
}

Below is my AcctChgRecType JAXB Object Class

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "AcctChgRecType", propOrder = {

})
public class AcctChgRecType {

    @XmlElement(name = "AccountType", required = true, nillable = true)
    protected String accountType;
    @XmlElement(name = "BalanceId", required = true, type = Long.class, nillable = true)
}

标签: java xml jaxb
1条回答
Deceive 欺骗
2楼-- · 2019-03-04 03:05

I would like to refer you to the following answer by @MichaelKay:

https://stackoverflow.com/a/50300327/303810

It is not the same question, but it is the same answer:

In XML applications the principle should be:

(a) Receiving applications shouldn't care about the insignificant lexical detail of how the XML is written. (The best way of achieving this is to use a respectable XML parser to read the XML.)

(b) Writing applications should be free to use whatever lexical conventions they find convenient. (Which means you can use any respectable serialization library to write the XML.)

Basically, you should not care. If you do, there's something seriously wrong with the way you process XML. It must not matter how namespaces are declared. Why should it?

To answer your question, this is not possible with standard JAXB.

查看更多
登录 后发表回答