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

查看:970
本文介绍了更改JAXB marshaller生成的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 marshaller获取所需标题的见解吗?

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:

  • https://java.net/jira/browse/JAXB/

你可以随时这样做:

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)

  • http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html

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

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