how to avoid JAXB extracting element's namespace and put it at the place of root element

限于喜欢 提交于 2019-12-24 16:22:00

问题


please help me with following question:

I am using Jersey and I have one XML, this XML comes with a class named "Definition" with well XML annotation annotated, the XML looks like:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tosca:Definitions xmlns:tosca="http://docs.oasis-open.org/tosca/ns/2011/12" targetNamespace="http://www.example.org/" id="winery-defs-for_ex1-MediaWiki">
    <tosca:ServiceTemplate targetNamespace="http://www.example.org/" name="MediaWiki" id="MediaWiki">
        <ns3:Properties xmlns:ns3="http://www.opentosca.org/winery/extensions/tosca/2013/02/12" xmlns:ns4="http://www.eclipse.org/winery/model/selfservice">
        </ns3:Properties>
        <tosca:TopologyTemplate>
            <tosca:NodeTemplate xmlns:ns3="http://www.example.com" xmlns:ns4="http://www.opentosca.org/winery/extensions/tosca/2013/02/12" name="MediaWiki_WebApp" type="ns3:MediaWiki_WebApp" id="MediaWiki_WebApp" ns4:x="496" ns4:y="7">
                <tosca:Policies>
                    <tosca:Policy xmlns:ns5="http://www.example.org/PolicyTemplates" xmlns:ns6="http://example.com/PolicyTypes" policyRef="ns5:PerformancePolicyTemplate" policyType="ns6:PerformancePolicyType" name="policy"/>
                </tosca:Policies>
            </tosca:NodeTemplate>
            <tosca:NodeTemplate xmlns:ns3="http://www.example.com" xmlns:ns4="http://www.opentosca.org/winery/extensions/tosca/2013/02/12" name="WikiDB_MySQLDB" type="ns3:WikiDB_MySQLDB" id="WikiDB_MySQLDB" ns4:x="841" ns4:y="145">
                <tosca:Policies>
                    <tosca:Policy xmlns:ns5="http://www.example.org/PolicyTemplates" xmlns:ns6="http://example.com/PolicyTypes" policyRef="ns5:PerformancePolicyTemplate" policyType="ns6:PerformancePolicyType" name="2"/>
                </tosca:Policies>
            </tosca:NodeTemplate>
            <tosca:NodeTemplate xmlns:ns3="http://www.example.com" xmlns:ns4="http://www.opentosca.org/winery/extensions/tosca/2013/02/12" name="MediaWiki_PHPApp" type="ns3:MediaWiki_PHPApp" id="MediaWiki_PHPApp" ns4:x="183" ns4:y="84"/>
            <tosca:RelationshipTemplate xmlns:ns3="http://www.example.com" name="con_337" type="ns3:ConsistsOf" id="con_337">
                <tosca:SourceElement ref="MediaWiki_WebApp"/>
                <tosca:TargetElement ref="WikiDB_MySQLDB"/>
            </tosca:RelationshipTemplate>
            <tosca:RelationshipTemplate xmlns:ns3="http://www.example.com" name="con_346" type="ns3:ConsistsOf" id="con_346">
                <tosca:SourceElement ref="MediaWiki_WebApp"/>
                <tosca:TargetElement ref="MediaWiki_PHPApp"/>
            </tosca:RelationshipTemplate>
            <tosca:RelationshipTemplate xmlns:ns3="http://www.example.com" name="con_358" type="ns3:InteractsWith" id="con_358">
                <tosca:SourceElement ref="MediaWiki_PHPApp"/>
                <tosca:TargetElement ref="WikiDB_MySQLDB"/>
            </tosca:RelationshipTemplate>
            <tosca:RelationshipTemplate xmlns:ns3="http://docs.oasis-open.org/tosca/ns/2011/12/ToscaBaseTypes" name="WikiDB_MySQLDB_tbtHostedOn_MySQLServer" type="ns3:HostedOn" id="WikiDB_MySQLDB_tbtHostedOn_MySQLServer">
                <tosca:SourceElement ref="WikiDB_MySQLDB"/>
                <tosca:TargetElement ref="MySQLServer"/>
            </tosca:RelationshipTemplate>
            <tosca:RelationshipTemplate xmlns:ns3="http://docs.oasis-open.org/tosca/ns/2011/12/ToscaBaseTypes" name="MediaWiki_PHPApp_tbtHostedOn_ApachePHPModule" type="ns3:HostedOn" id="MediaWiki_PHPApp_tbtHostedOn_ApachePHPModule">
                <tosca:SourceElement ref="MediaWiki_PHPApp"/>
                <tosca:TargetElement ref="ApachePHPModule"/>
            </tosca:RelationshipTemplate>
            <tosca:NodeTemplate xmlns:ns3="http://www.example.com" xmlns:ns4="http://www.opentosca.org/winery/extensions/tosca/2013/02/12" name="MySQLServer" type="ns3:MySQLServer" id="MySQLServer" ns4:x="841" ns4:y="338"/>
            <tosca:RelationshipTemplate xmlns:ns3="http://docs.oasis-open.org/tosca/ns/2011/12/ToscaBaseTypes" name="MySQLServer_tbtHostedOn_UbuntuVM_11" type="ns3:HostedOn" id="MySQLServer_tbtHostedOn_UbuntuVM_11">
                <tosca:SourceElement ref="MySQLServer"/>
                <tosca:TargetElement ref="UbuntuVM_11"/>
            </tosca:RelationshipTemplate>
            <tosca:NodeTemplate xmlns:ns3="http://www.opentosca.org/types/nodetypes" xmlns:ns4="http://www.opentosca.org/winery/extensions/tosca/2013/02/12" name="UbuntuVM" type="ns3:UbuntuVM" id="UbuntuVM_11" ns4:x="517" ns4:y="338">
                <tosca:Properties>
                    <ns5:Properties xmlns:ns5="http://www.opentosca.org/types/nodetypes/propertiesdefinition/winery" xmlns:ns6="http://www.eclipse.org/winery/model/selfservice">
                        <ns5:SSHUser/>
                        <ns5:SSHKey/>
                        <ns5:ImageID/>
                        <ns5:IPAddress/>
                        <ns5:Size/>
            </ns5:Properties>
                </tosca:Properties>
            </tosca:NodeTemplate>
            <tosca:RelationshipTemplate xmlns:ns3="http://docs.oasis-open.org/tosca/ns/2011/12/ToscaBaseTypes" name="UbuntuVM_11_tbtHostedOn_AmazonEC2_2" type="ns3:HostedOn" id="UbuntuVM_11_tbtHostedOn_AmazonEC2_2">
                <tosca:SourceElement ref="UbuntuVM_11"/>
                <tosca:TargetElement ref="AmazonEC2_2"/>
            </tosca:RelationshipTemplate>
            <tosca:NodeTemplate xmlns:ns3="http://www.opentosca.org/types/nodetypes" xmlns:ns4="http://www.opentosca.org/winery/extensions/tosca/2013/02/12" name="AmazonEC2" type="ns3:AmazonEC2" id="AmazonEC2_2" ns4:x="517" ns4:y="476">
                <tosca:Properties>
                    <ns5:Properties xmlns:ns5="http://www.opentosca.org/types/nodetypes/propertiesdefinition/winery" xmlns:ns6="http://www.eclipse.org/winery/model/selfservice">
                        <ns5:Password/>
                        <ns5:Account/>
            </ns5:Properties>
                </tosca:Properties>
            </tosca:NodeTemplate>
            <tosca:NodeTemplate xmlns:ns3="http://docs.oasis-open.org/tosca/ns/2011/12/ToscaSpecificTypes" xmlns:ns4="http://www.opentosca.org/winery/extensions/tosca/2013/02/12" name="ApachePHPModule" type="ns3:ApachePHPModule" id="ApachePHPModule" ns4:x="183" ns4:y="215">
                <tosca:Properties>
                    <ns5:Properties xmlns:ns5="http://docs.oasis-open.org/tosca/ns/2011/12/ToscaSpecificTypes/propertiesdefinition/winery" xmlns:ns6="http://www.eclipse.org/winery/model/selfservice"/>
                </tosca:Properties>
            </tosca:NodeTemplate>
            <tosca:RelationshipTemplate xmlns:ns3="http://docs.oasis-open.org/tosca/ns/2011/12/ToscaBaseTypes" name="ApachePHPModule_tbtHostedOn_ApacheWebserver" type="ns3:HostedOn" id="ApachePHPModule_tbtHostedOn_ApacheWebserver">
                <tosca:SourceElement ref="ApachePHPModule"/>
                <tosca:TargetElement ref="ApacheWebserver"/>
            </tosca:RelationshipTemplate>
            <tosca:NodeTemplate xmlns:ns3="http://www.opentosca.org/types/nodetypes" xmlns:ns4="http://www.opentosca.org/winery/extensions/tosca/2013/02/12" name="ApacheWebserver" type="ns3:ApacheWebserver" id="ApacheWebserver" ns4:x="183" ns4:y="338">
                <tosca:Properties>
                    <ns5:Properties xmlns:ns5="http://www.opentosca.org/types/nodetypes/propertiesdefinition/winery" xmlns:ns6="http://www.eclipse.org/winery/model/selfservice">
                        <ns5:HTTPPort/>
            </ns5:Properties>
                </tosca:Properties>
            </tosca:NodeTemplate>
            <tosca:RelationshipTemplate xmlns:ns3="http://docs.oasis-open.org/tosca/ns/2011/12/ToscaBaseTypes" name="ApacheWebserver_tbtHostedOn_UbuntuVM_11" type="ns3:HostedOn" id="ApacheWebserver_tbtHostedOn_UbuntuVM_11">
                <tosca:SourceElement ref="ApacheWebserver"/>
                <tosca:TargetElement ref="UbuntuVM_11"/>
            </tosca:RelationshipTemplate>
        </tosca:TopologyTemplate>
    </tosca:ServiceTemplate>
</tosca:Definitions>

The problem is that every time I will get several this kind of result,so I put it in a new class, and I put an ID for each "Definition",my new class looks like this(it is generated from xsd by xjc):

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

        protected List<MuTopologies.Specification> specification;


        public List<MuTopologies.Specification> getSpecification() {
            if (specification == null) {
                specification = new ArrayList<MuTopologies.Specification>();
            }
            return this.specification;
        }



        @XmlAccessorType(XmlAccessType.FIELD)
        @XmlType(name = "", propOrder = {
            "definitions"
        })
        public static class Specification {

            @XmlElement(name = "Definitions", required = true,namespace="http://docs.oasis-open.org/tosca/ns/2011/12")
            protected org.eclipse.toscaModel.Definitions definitions;
            @XmlAttribute(name = "id", required = true)
            protected int id;


            public org.eclipse.toscaModel.Definitions getDefinitions() {
                return definitions;
            }


            public void setDefinitions(org.eclipse.toscaModel.Definitions value) {
                this.definitions = value;
            }


            public int getId() {
                return id;
            }


            public void setId(int value) {
                this.id = value;
            }



            @XmlAccessorType(XmlAccessType.FIELD)
            @XmlType(name = "")
            public static class Definitions
                extends TDefinitions
            {


            }

        }

    }

Then I use JAXB to marshal it, I can get almost the perfect result,looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MuTopologies xmlns:tosca="http://docs.oasis-open.org/tosca/ns/2011/12">
    <specification id="1">
        <tosca:Definitions targetNamespace="http://www.example.org/" id="winery-defs-for_ex1-MediaWiki">
           ........
        </tosca:Definitions>
    </specification>
    <specification id="2">
        <tosca:Definitions targetNamespace="http://www.example.org/" id="winery-defs-for_ex1-MediaWiki">
           ........
        </tosca:Definitions>
    </specification>
</MuTopologies>

The only question is that my namespace in the "Definitions" is moved to the root element of my new XML(xmlns:tosca="http://docs.oasis-open.org/tosca/ns/2011/12" ), I do not want this, I want like this:

   <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <MuTopologies >
        <specification id="1">
            <tosca:Definitions xmlns:tosca="http://docs.oasis-open.org/tosca/ns/2011/12" targetNamespace="http://www.example.org/" id="winery-defs-for_ex1-MediaWiki">
               ........
            </tosca:Definitions>
        </specification>
        <specification id="2">
            <tosca:Definitions xmlns:tosca="http://docs.oasis-open.org/tosca/ns/2011/12" targetNamespace="http://www.example.org/" id="winery-defs-for_ex1-MediaWiki">
               ........
            </tosca:Definitions>
        </specification>
    </MuTopologies>

Is there a method to avoid JAXB extracting the namespace of Element? if not, is there a way to wrap my every "Definition" xml in a new xml??

Thanks very much. I have try to solve it for the whole weekend, it makes me crazy. thanks.

来源:https://stackoverflow.com/questions/33860128/how-to-avoid-jaxb-extracting-elements-namespace-and-put-it-at-the-place-of-root

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!