2

我正在尝试使用具有以下格式的 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=""> 

FormSubmission并由CompanyCorporation生成 。@XmlRootElement_xjc

xmlns 和 schemalocation 值的相关 @XmlSchema 属性已添加到 package-info.java 中,也按照此处@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;   

我如何生成它们schemalocations以及xmlns网关在哪里需要它们?

4

3 回答 3

1

你需要@XmlSchema在你的包上使用注解。

您可以通过添加package-info.java到与 JAXB 注释类相同的包中来做到这一点。

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

于 2013-08-04T04:02:43.420 回答
0

您可以设置Marshaller.JAXB_SCHEMA_LOCATIONonMarshaller以输出schemaLocation

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

了解更多信息

于 2013-08-06T00:51:24.887 回答
0

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

@XmlAttribute(name="namespace_name")
public String namespace_value="xxxxxxxxxx";
于 2013-09-07T12:22:10.703 回答