如何避免JAXB提取元素的命名空间并将其放在根元素的位置

时间:2015-11-22 21:00:28

标签: xml parsing jaxb jersey

请帮我解决以下问题:

我正在使用Jersey,我有一个XML,这个XML带有一个名为"定义"的类。注释好的XML注释,XML看起来像:

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

问题是,每次我得到几个这样的结果,所以我把它放在一个新的类中,我为每个&#34;定义&#34;,我的新类看起来像这样(它是由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
            {


            }

        }

    }

然后我使用JAXB编组它,我几乎可以得到完美的结果,看起来像这样:

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

唯一的问题是我的命名空间在&#34;定义&#34;移动到我的新XML的根元素(xmlns:tosca =&#34; http://docs.oasis-open.org/tosca/ns/2011/12"),我不想要这个,我想要这样:

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

有没有一种方法可以避免JAXB提取Element的命名空间?如果没有,有没有办法包装我的每一个&#34;定义&#34;新的xml中的xml ??

非常感谢。我试着在整个周末解决它,这让我发疯。感谢。

0 个答案:

没有答案