小编Sim*_*ing的帖子

在 JaxB 编组没有 @XmlRootElement 注释的元素时删除 ns2 前缀

我有一个要编组的对象,但架构没有 @XmlRootElement 注释。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
public static class Foo
{
    @XmlAttribute(name = "test1")
    public final static String TEST_1 = "Foo";

    @XmlElement(name = "Element1", required = true)
    protected String element1;

    @XmlElement(name = "Element2", required = true)
    protected String element2;
}
Run Code Online (Sandbox Code Playgroud)

我通过在编组时指定 JaxBElement 来编组对象

QName qName = new QName("", "Foo");
jaxb2Marshaller.marshal(new JAXBElement(qName, Foo.class, fooObj), new StreamResult(baos));
Run Code Online (Sandbox Code Playgroud)

这将在编组后产生以下 XML

<Foo xmlns:ns2="http://Foo/bar" test1="Foo">
    <ns2:Element1>000000013</ns2:Element1>
    <ns2:Element2>12345678900874357</ns2:Element2>
</Foo>
Run Code Online (Sandbox Code Playgroud)

对于我的用例,我想在没有 ns2 前缀的情况下编组这个对象,以便 XML 看起来像

<Foo xmlns="http://Foo/bar" test1="Foo">
    <Element1>000000013</Element1>
    <Element2>12345678900874357</Element2>
</Foo>
Run Code Online (Sandbox Code Playgroud)

如何在没有前缀的情况下编组这个对象?

提前致谢。

java xml jaxb prefix

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

标签 统计

java ×1

jaxb ×1

prefix ×1

xml ×1