I have got problem with JAXB marshalling in Java 1.6_u33
I`ve got 5 schemas .xsd which are used for generating Java classes and then marshalling XML file.
Problem is with only one case – for this file JAXB generate additional namespace prefix ns2. This is very strange because all schemas are identical and marshalling mechnism is generic for all of them.
Generation mechanism:
JAXBContext context = JAXBContext.newInstance(type);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
Boolean.TRUE);
marshaller.marshal(file, doc);
And the first lines of bad xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ns2:Document xmlns:ns2="urn:iso:std:iso:20022:tech:xsd:camt.056.001.01">
<ns2:camt.056.001.01>
<ns2:Assgnmt>
<ns2:Id>NOTPROVIDED</ns2:Id>
<ns2:CreDtTm>2008-06-24T00:00:00</ns2:CreDtTm>
</ns2:Assgnmt>
With the same settings xml generated from another xsd is:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.029.001.03">
<camt.029.001.03>
<Assgnmt>
<Id>NOTPROVIDED</Id>
<CreDtTm>2008-03-26T00:00:00</CreDtTm>
</Assgnmt>
I would be very grateful for any help… Thanks.
I couldn`t add answer to my question so I add here my explanation.
For not properly working package:
Package-info:
@javax.xml.bind.annotation.XmlSchema(namespace = "urn:iso:std:iso:20022:tech:xsd:camt.056.001.01", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package eu.axabank.axaconverter.datamodel.camt056;
Object-factory
package eu.axabank.axaconverter.datamodel.camt056;
import javax.xml.bind.annotation.XmlRegistry;
/**
* This object contains factory methods for each
* Java content interface and Java element interface
* generated in the eu.axabank.axaconverter.datamodel.camt056 package.
* <p>An ObjectFactory allows you to programatically
* construct new instances of the Java representation
* for XML content. The Java representation of XML
* content can consist of schema derived interfaces
* and classes representing the binding of schema
* type definitions, element declarations and model
* groups. Factory methods for each of these are
* provided in this class.
*
*/
@XmlRegistry
public class ObjectFactory {
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: eu.axabank.axaconverter.datamodel.camt056
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {@link CaseAssignmentBIC }
*
*/
public CaseAssignmentBIC createCaseAssignmentBIC() {
return new CaseAssignmentBIC();
}
/**
* Create an instance of {@link UnderlyingTransaction }
*
*/
public UnderlyingTransaction createUnderlyingTransaction() {
return new UnderlyingTransaction();
}
/**
* Create an instance of {@link PaymentTransactionInformation }
*
*/
public PaymentTransactionInformation createPaymentTransactionInformation() {
return new PaymentTransactionInformation();
}
/**
* Create an instance of {@link RemittanceInformation }
*
*/
public RemittanceInformation createRemittanceInformation() {
return new RemittanceInformation();
}
/**
* Create an instance of {@link OriginalTransactionReference }
*
*/
public OriginalTransactionReference createOriginalTransactionReference() {
return new OriginalTransactionReference();
}
/**
* Create an instance of {@link ControlData }
*
*/
public ControlData createControlData() {
return new ControlData();
}
/**
* Create an instance of {@link Document }
*
*/
public Document createDocument() {
return new Document();
}
/**
* Create an instance of {@link CancellationReasonInformationBICorName }
*
*/
public CancellationReasonInformationBICorName createCancellationReasonInformationBICorName() {
return new CancellationReasonInformationBICorName();
}
/**
* Create an instance of {@link ActiveOrHistoricCurrencyAndAmountEUR }
*
*/
public ActiveOrHistoricCurrencyAndAmountEUR createActiveOrHistoricCurrencyAndAmountEUR() {
return new ActiveOrHistoricCurrencyAndAmountEUR();
}
/**
* Create an instance of {@link Camt056 }
*
*/
public Camt056 createCamt056() {
return new Camt056();
}
}
For good working package:
package-info:
@javax.xml.bind.annotation.XmlSchema(namespace = "urn:iso:std:iso:20022:tech:xsd:camt.029.001.03", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package eu.axabank.axaconverter.datamodel.camt029;
And object factory looks exactly the same (except naming of course).
I have tried QName element – it is inconvenient due the generic nature of mechanizm – but withount any good result.
I was using NamespacePrefixMapper:
NamespacePrefixMapper mapper = new NamespacePrefixMapper() {
public String getPreferredPrefix(String namespaceUri,
String suggestion, boolean requirePrefix) {
return "";
}
};
marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper",
mapper);
but this mapper did`t work! He was setting – but method getPreferredPrefix was never called during mershalling.
I don`t understand different beetwen my 4 schemas and this not working one…
Problem detected!
I am confused but it was my mistake.
To get down to brass tacks:
I generated 5 packages of Java classes from 5 different schemas. They were importing however one schema with simple types (common objects for schemas). I this common XSD I placed mainly objects….yeah mainly. There were 2 elements…unfortunately. This complexTypes were used only by object generated in this one – not working – case.
The point is that 2 objects was generating from another Objectfactory – placed in another package!
I deduce that mixing objects from more than one Object factory in one marshalling object couse problem descrbed in my first post.
Clue is: If you dont want have many namespaces in XML u should never use two Object factories or import schemas with complex type.