java - Jaxb:根元素中的 xsi 被替换为 ns2

标签 java namespaces jaxb marshalling

我使用了以下代码片段

 marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
            Boolean.TRUE);
        marshaller.setSchema(getSchema(xsdSchema));
        marshaller.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper",
            new NamespacePrefixMapper() {
                @Override
                public String getPreferredPrefix(String arg0, String arg1,
                    boolean arg2) {
                    return "tf";
                }
            });
        marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
            "http://www.xyz.com/tf " + xsdSchema);

        marshaller.marshal(obj, new StreamResult(xml));

输出xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tf:abc xmlns:tf="http://www.xyz.com/tf" xmlns:ns2="http://www.w3.org/2001/XMLSchema-instance" ns2:schemaLocation="http://www.xyz.com/tf schema/myxsd.xsd">

如您所见,我用“ns2”代替了“xsi”。

我需要的是 xsi 代替 ns2。

提前致谢。

最佳答案

您的命名空间前缀映射器应返回声明的每个命名空间的值:

package mapdemo;

import com.sun.xml.internal.bind.marshaller.NamespacePrefixMapper;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

public class JAXBMarshallerExample {

    public static NamespacePrefixMapper val = new NamespacePrefixMapper() {

        private static final String XSI_URI = "http://www.w3.org/2001/XMLSchema-instance";
        private static final String TF_URI = "http://www.xyz.com/tf/whatever.xsd";

        @Override
        public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
            if(XSI_URI.equals(namespaceUri)) {
                return "xsi";
            } else if(TF_URI.equals(namespaceUri)) {
                return "tf";
            }
            return suggestion;
        }

        @Override
        public String[] getPreDeclaredNamespaceUris() {
            return new String[] { XSI_URI, TF_URI};
        }
    };

    public static void main(String[] args) throws Exception {

        JAXBContext context = JAXBContext.newInstance(Output.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper", val);

        Output output = new Output();
        output.setId("1");

        marshaller.marshal(output, System.out);
    }

    @XmlRootElement(namespace="http://www.xyz.com/tf/whatever.xsd")
    public static class Output {
        String id;

        public String getId() {
            return id;
        }

        @XmlElement
        public void setId(String id) {
            this.id = id;
        }
    }
}

关于java - Jaxb:根元素中的 xsi 被替换为 ns2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18779524/

相关文章:

java - 使用 DOM 将 XML 解析为 HashMap

java - 使用类加载器加载内部类

php - 如何找出需要在 Symfony 4 中添加的 use 语句?

c# - 找不到类型或命名空间名称,C# 初学者

java - 从 QName 获取 Java 类名

java - 如何避免嵌套的 for-each 循环?

java - Java 是否有与 boost 或 Qt 信号等效的信号?

java - Java 中的意外负数

c++使用ncurses库重复函数名称

java - 当消息是 XML 格式时,如何在 Camel 选择组件中使用 simple ?