Jaxb: Generating XML with different root element included in class which is being marshalled?

StackOverflow https://stackoverflow.com/questions/18036811

  •  23-06-2022
  •  | 
  •  

Question

I am trying to create Companies House XML using JAXB which has following format

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

XML structure has been generated correctly apart from schemalocations and xlmns, schemalocation are not generation for any of the element where it should be and all xmlns of are visible in <GovtTalkMessage> like

 <GovtTalkMessage  schemaLocation="schema1" xlmns="" xlmns:bs= "" xlmns:gt="" xlmns:xx="" xlmns:yy=""> 

FormSubmission and CompanyCorporation are generated as @XmlRootElement by xjc.

Relevant @XmlSchema properties for xmlns and schemalocation value are added in package-info.java , also tried @XmlElementDecl as suggested here but it did not help me.

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;

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

How can I generate schemalocations and xmlns where they are required by Gateway?

Was it helpful?

Solution 3

This problem have been resolved by adding @XmlAttribute in corresponding @XmlRootElement classes.

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

OTHER TIPS

You need to use the @XmlSchema annotation on your package.

You can do this by adding package-info.java to the same package as your JAXB-annotated classes.

See the javadoc for examples: http://docs.oracle.com/javaee/6/api/javax/xml/bind/annotation/XmlSchema.html

You can set the Marshaller.JAXB_SCHEMA_LOCATION on the Marshaller to output the schemaLocation:

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

For More Information

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top