相关疑难解决方法(0)

从JAXB Generics中删除xsi:type,xmlns:xs和xmlns:xsi

使用JAXB时,我想在使用Generics时从我的XML元素中删除多余的命名空间/类型.我怎么能这样做或我做错了什么?我想使用泛型,所以我只需要编写一段代码.

示例代码:

public static void main(String[] args) {
    try {
        TestRoot root = new TestRoot();
        root.name.value = "bobby";
        root.age.value = 102;
        root.color.value = "blue";

        JAXBContext context = JAXBContext.newInstance(root.getClass());
        Marshaller marsh = context.createMarshaller();
        marsh.setProperty(Marshaller.JAXB_ENCODING,"UTF-8");
        marsh.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE);

        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        marsh.marshal(root,pw);
        System.out.println(sw.toString());
    }
    catch(Throwable t) {
        t.printStackTrace();
    }
}

@XmlRootElement
static class TestRoot {
    @XmlElement public TestGeneric<String> name = new TestGeneric<String>(true);
    @XmlElement public TestGeneric<Integer> age = new TestGeneric<Integer>(true);
    @XmlElement public TestWhatIWouldLike color = new TestWhatIWouldLike(true);
} …
Run Code Online (Sandbox Code Playgroud)

java generics jaxb xml-namespaces xsi

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

generics ×1

java ×1

jaxb ×1

xml-namespaces ×1

xsi ×1