Jaxb:使用包含在正在编组的类中的不同根元素生成XML?

时间:2013-08-03 20:05:34

标签: java jaxb jaxb2

我正在尝试使用具有以下格式

的JAXB创建Companies House XML
<GovtTalkMessage  schemaLocation="schema1" xlmns="" xlmns:bs= "" xlmns:gt="">  
    <Header>    ....    </Header>    
   <Body>
       <FormSubmission schemaLocation="schema2" xlmns="" xlmns:xx="">
          .....
         <CompanyIncorporation schemaLocation="schema3" xlmns="" xlmns:yy="">
                 ...
          </CompanyIncorporation>
        </FormSubmission>    
   </Body> 
</GovtTalkMessage>

除了 schemalocations和xlmns 之外,XML结构已经正确生成,对于任何应该存在的元素都没有生成schemalocation,并且所有xmlns都在<GovtTalkMessage>中可见

 <GovtTalkMessage  schemaLocation="schema1" xlmns="" xlmns:bs= "" xlmns:gt="" xlmns:xx="" xlmns:yy=""> 
{p> FormSubmissionCompanyCorporation@XmlRootElement生成为xjc

xmlns和schemalocation值的相关@XmlSchema属性添加在package-info.java中,也按照建议here尝试了@XmlElementDecl,但它对我没有帮助。

CompanyIncorportion package-info.java

@XmlSchema(namespace = "",   
    xmlns = {    
        @XmlNs(namespaceURI = "http://xmlgw.companieshouse.gov.uk", prefix = ""),   
        @XmlNs(namespaceURI = "http://www.w3.org/2001/XMLSchema-instance" , prefix="xsi")
   }, 
    location = "http://xmlgw.companieshouse.gov.uk http://xmlgw.companieshouse.gov.uk/v2-1/schema/forms/CompanyIncorporation-v2-6.xsd",
    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.UNSET) 
package com.xx.ch;
import javax.xml.bind.annotation.XmlNs;   
import javax.xml.bind.annotation.XmlSchema;

网关package-info.java:

 @XmlSchema(namespace = "",   
        xmlns = {    
            @XmlNs(namespaceURI = "http://www.w3.org/2000/09/xmldsig#", prefix = "dsig"),   
            @XmlNs(namespaceURI = "http://www.govtalk.gov.uk/schemas/govtalk/core", prefix = "gt"),   
            @XmlNs(namespaceURI = "http://www.w3.org/2001/XMLSchema-instance" , prefix="xsi")
       },   
        location = "http://www.govtalk.gov.uk/CM/envelope http://xmlgw.companieshouse.gov.uk/v1-0/schema/Egov_ch-v2-0.xsd",
     elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)   

    package com.xx.gateway;  
    import javax.xml.bind.annotation.XmlNs;   
    import javax.xml.bind.annotation.XmlSchema;

FormSubmission package-info.java

@XmlSchema(namespace = "",   
    xmlns = {    
        @XmlNs(namespaceURI = "http://xmlgw.companieshouse.gov.uk", prefix = "bs"),   
        @XmlNs(namespaceURI = "http://xmlgw.companieshouse.gov.uk/Header", prefix = ""),   
        @XmlNs(namespaceURI = "http://www.w3.org/2001/XMLSchema-instance" , prefix="xsi")
   }, 
   // location ="http://xmlgw.companieshouse.gov.uk/Header http://xmlgw.companieshouse.gov.uk/v2-1/schema/forms/FormSubmission-v2-7.xsd" ,
       //@javax.xml.bind.annotation.XmlSchema(namespace = "http://xmlgw.companieshouse.gov.uk/Header", 
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
    package com.xx.formsubmission;
    import javax.xml.bind.annotation.XmlNs;   

如何在网关需要的地方生成schemalocationsxmlns

3 个答案:

答案 0 :(得分:1)

您需要在包装上使用@XmlSchema注释。

您可以通过将package-info.java添加到与JAXB注释的类相同的包中来实现此目的。

有关示例,请参阅javadoc: http://docs.oracle.com/javaee/6/api/javax/xml/bind/annotation/XmlSchema.html

答案 1 :(得分:0)

您可以在Marshaller.JAXB_SCHEMA_LOCATION上设置Marshaller以输出schemaLocation

marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.example.com/address address.xsd")

了解更多信息

答案 2 :(得分:0)

通过在相应的@XmlAttribute类中添加@XmlRootElement已解决此问题。

@XmlAttribute(name="namespace_name")
public String namespace_value="xxxxxxxxxx";