更改 JAXB 编组器生成的 XML 标头 [英] Altering the XML header produced by the JAXB marshaller

查看:37
本文介绍了更改 JAXB 编组器生成的 XML 标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用以下代码将对象编组为 xml 字符串

I am currently using the following code to marshal an object into an xml string

    JAXBContext context;

    try {
        context = JAXBContext.newInstance(heartbeat.getClass());
        StringWriter writer = new StringWriter();
        Marshaller marshaller = context.createMarshaller();

        heartbeat.setHeader(header);
        heartbeat.setHeartbeatEvent(event);

        marshaller.marshal(heartbeat, writer);
        String stringXML = writer.toString();
        return stringXML;

    } catch (JAXBException e) {
        throw new RuntimeException("Problems generating XML in specified "
                + "encoding, underlying problem is " + e.getMessage(),
                e);
    }

产生以下标题

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

我想要的输出如下

<?xml version=\"1.0\"?>

通过将其添加到编组器

marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
marshaller.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.0\"?>");

我收到

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><?xml version="1.0"?>

并将 JAXB_FRAGMENT 属性更改为 TRUE 会完全删除标头.我一直在关注 JAXB - 删除 'standalone="yes"'从生成的 XML 线程尝试解决问题,但到目前为止我没有运气.有人能给我一些关于如何从 JAXB 编组器获取我想要的标头的见解吗?

and changing the JAXB_FRAGMENT property to TRUE removes the header entirely. I have been following the JAXB - Remove 'standalone="yes"' from generated XML thread attempting to solve the problem but I have had no luck so far. Can someone please give me some insight on how to get my desired header from the JAXB marshaller?

推荐答案

使用以下组合编组到 OutputStream 时会产生预期的输出.

When marshalling to an OutputStream using a combination of the following produces the expected output.

    marshaller.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.0\"?>");
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

当您编组到 Writer 时会出现您所看到的问题,这似乎是 JAXB 参考实现中的错误.您可以通过以下链接提出问题:

The problem you are seeing occurs when you marshal to a Writer, which appears to be a bug in the JAXB reference implementation. You can raise an issue at the link below:

你总是可以这样做:

JAXBContext context;

try {
    context = JAXBContext.newInstance(heartbeat.getClass());
    StringWriter writer = new StringWriter();
    writer.append("<?xml version=\"1.0\"?>");
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

    heartbeat.setHeader(header);
    heartbeat.setHeartbeatEvent(event);

    marshaller.marshal(heartbeat, writer);
    String stringXML = writer.toString();
    return stringXML;

} catch (JAXBException e) {
    throw new RuntimeException("Problems generating XML in specified "
            + "encoding, underlying problem is " + e.getMessage(),
            e);
}

<小时>

EclipseLink JAXB (MOXy) 也支持 com.sun.xml.bind.xmlHeaders 并且在编组到 Writer 时它可以正常工作(我是 MOXy 负责人)


EclipseLink JAXB (MOXy) also supports the com.sun.xml.bind.xmlHeaders and it works correctly when marshalling to a Writer (I'm the MOXy lead)

这篇关于更改 JAXB 编组器生成的 XML 标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆