【问题标题】:Additional namespace prefix during JAXB marshallingJAXB 编组期间的附加命名空间前缀
【发布时间】:2023-03-08 02:48:01
【问题描述】:

我在 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);

但是这个映射器没有工作!他正在设置 - 但在 mershalling 期间从未调用方法 getPreferredPrefix。

我不理解我的 4 种模式的不同之处,而这一种模式不起作用......

【问题讨论】:

  • package-info / objectfactory 中有什么?它与默认命名空间有关。
  • Replay for you Alexander 添加到原始帖子的末尾。
  • urn:iso:std:iso:20022:tech:xsd:camt.056.001.01.xsd,这里的命名空间/目标命名空间是什么?你能提供一个示例项目吗?
  • namespace 是我自己的名字,根据通用金融行业消息方案 ISO 200022 构建。重要吗?
  • 好吧,我错了。 xsd 在这里并不重要。在查看代码示例时我一无所知。

标签: xml namespaces jaxb marshalling


【解决方案1】:

检测到问题!

我很困惑,但这是我的错误。

要认真对待:

我从 5 个不同的模式生成了 5 个 Java 类包。然而,他们正在导入一种具有简单类型的模式(模式的通用对象)。我这个常见的 XSD 我主要放置对象....是的,主要是。有两个元素......不幸的是。此 complexTypes 仅由在此情况下生成的对象使用 - 不工作 - 情况。

关键是 2 个对象是从另一个 Objectfactory 生成的 - 放在另一个包中!

我推断在一个编组对象中混合来自多个对象工厂的对象会导致我的第一篇文章中描述的问题。

线索是:如果你不想在 XML 中有很多命名空间,你不应该使用两个对象工厂或导入具有复杂类型的模式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-05
    • 2011-09-11
    • 1970-01-01
    • 1970-01-01
    • 2013-10-24
    相关资源
    最近更新 更多