我在 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 元素 - 由于 mechanizm 的通用性质,它很不方便 - 但没有任何好的结果。
我正在使用 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 种模式的不同之处,而这不起作用...