我在Java 1.6_u33中遇到了JAXB编组问题
我有5个模式.xsd,用于生成Java类,然后编组XML文件 .
问题只有一种情况 - 对于这个文件,JAXB生成额外的名称空间前缀ns2 . 这很奇怪,因为所有模式都是相同的,并且编组机制对于所有模式都是通用的 .
生成机制:
JAXBContext context = JAXBContext.newInstance(type);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
Boolean.TRUE);
marshaller.marshal(file, doc);
而第一行的坏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>
使用相同的设置从另一个xsd生成的xml是:
<?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>
我会非常感谢任何帮助......谢谢 .
我无法在我的问题中添加答案,所以我在这里添加我的解释 .
对于不正常工作的包:
包信息:
@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;
对象的工厂
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();
}
}
对于良好的工作包:
包信息:
@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;
对象工厂看起来完全一样(当然命名除外) .
我尝试了QName元素 - 由于机制的一般性质而不方便 - 但是有任何好的结果 .
我使用的是NamespacePrefixMapper:
NamespacePrefixMapper mapper = new NamespacePrefixMapper() {
public String getPreferredPrefix(String namespaceUri,
String suggestion, boolean requirePrefix) {
return "";
}
};
marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper",
mapper);
但是这个映射器没有用!他正在设置 - 但是在编组过程中从未调用方法getPreferredPrefix .
我不明白我的4个模式的不同beet,这不工作...
1 回答
检测到问题!
我很困惑,但那是我的错 .
要了解黄铜钉:
我从5个不同的模式生成了5个Java类包 . 然而,他们正在导入一个具有简单类型的模式(模式的常用对象) . 我这个常见的XSD我主要放置物体......主要是 . 不幸的是,有2个元素 . 这个complexTypes仅由在这个中生成的对象使用 - 不工作 - 情况 .
关键是2个对象是从另一个Objectfactory生成的 - 放在另一个包中!
我推断,在我的第一篇文章中描述了在一个编组对象中混合来自多个对象工厂的对象问题 .
线索是: If you dont want have many namespaces in XML u should never use two Object factories or import schemas with complex type.