每个xml标记上的JAXB命名空间 [英] JAXB Namespace on each xml tag

查看:145
本文介绍了每个xml标记上的JAXB命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jaxb生成XML请求。以下是所有代码详细信息。我也尝试过package-info,但它对我没用。就像我想在每个对象XML标签上添加命名空间一样。

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.

希望你们明白我的观点。我做错了什么或遗失了什么?或者这种事情在JAXB Java中是否可行。

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.

必需输出

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

输出我的

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

以下是我的JAXB父Java对象类

@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;
}

以下是我的内部JAXB对象Java类

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

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

以下是我的AcctChgRecType JAXB对象类

@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)
}


推荐答案

我想通过@MichaelKay向您推荐以下答案:

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:


在XML应用程序中,原则应该是:

In XML applications the principle should be:

(a)接收应用程序不应该关心如何编写XML的无关紧要的词法细节。 (实现这一目标的最佳方法是使用一个值得尊重的XML解析器来读取XML。)

(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)编写应用程序应该可以自由使用他们找到的任何词汇约定方便。 (这意味着您可以使用任何可敬的序列化库来编写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.)

基本上,您不应该在意。如果这样做,那么处理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?

要回答您的问题,标准JAXB无法做到这一点。

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

这篇关于每个xml标记上的JAXB命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆