JAXB 分段编组 [英] JAXB Fragmented Marshalling

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

问题描述

我在使用 Marshaller.JAXB_FRAGMENT 属性成功编组时遇到了一些麻烦.这是我尝试输出的 XML 的简单版本.

I'm having some trouble successfully marshalling using the Marshaller.JAXB_FRAGMENT property. Here's a simple version of the XML i'm trying to output.

<Import>
    <WorkSets>
        <WorkSet>
            <Work>
            <Work>
            ...
            ..
            ...
        </WorkSet>
        <WorkSet>
            <Work>
            <Work>
            ...
        </WorkSet>
    <WorkSets>
<Import>

元素本质上只是包含大量 的容器元素& 元素.我目前正在尝试在 上进行编组.

The <Import> and <WorkSets> elements are essentially just container elements that enclose a large number of <WorkSet> & <Work> elements. I'm currently trying to marshall at the <WorkSet>.

  1. 是否可以先对 元素进行编组,然后再在 <进行编组?/code> 元素并将输出括在 标签中?
  2. 当我在 WorkSet 级别进行编组时,它会将 xmlns='http://namespace.com' 属性附加到 WorkSet 标记,是否有一种方法可以在没有命名空间属性的情况下进行编组附加到工作集?
  1. Is it possible to initially marshal the <Import> and <WorkSets> elements and then from then on marshal at the <WorkSet> element and have the output be enclosed in the <Import><WorkSets> tags?
  2. When I'm marshaling at the WorkSet level it attaches the xmlns='http://namespace.com' attribute to the WorkSet tag, is there a way to marshal without the namespace attribute being attached to Workset?

推荐答案

基本上,与其用容器对象构建一个完整的对象树,不如说您希望能够使用 JAXB 流式传输一组 WorkSet 实例以进行编组.

Basically, it sounds like rather than constructing a full object tree with the container objects, you want to be able to stream a collection of WorkSet instances to marshal using JAXB.

我将采用的方法是使用 XMLStreamWriter 并通过将 WorkSet 对象包装在 JAXBElement 中来编组它们.我手头没有测试过示例代码,所以这里是粗略的代码片段,可以让您进入编写轨道:

The approach I would take is to use an XMLStreamWriter and marshal the WorkSet objects by wrapping them in a JAXBElement. I don't have tested sample code close at hand, so here's the rough code snippet that should put you on the write track:

FileOutputStream fos = new FileOutputStream("foo.xml");
XMLStreamWriter writer = XMLOutputFactory.newFactory().createXMLStreamWriter(fos);

writer.writeStartDocument();
writer.writeStartElement("Import");
writer.writeStartElement("WorkSets");

JAXBContext context = JAXBContext.newInstance(WorkSet.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); 
for (WorkSet instance : instances)
{
    JAXBElement<WorkSet> element = new JAXBElement<WorkSet>(QName.valueOf("WorkSet"), WorkSet.class, instance);
    m.marshal(element, writer);
}

writer.writeEndDocument(); // this will close any open tags
writer.close();

注意: 以上内容完全未经测试,可能会在包装部分弄乱一些东西来编写 WorkSet 的每个实例.您需要包装 WorkSet 实例,因为它们不会使用 @XmlRootElement 进行注释,否则 JAXB 将拒绝封送对象.

Note: The above is completely untested and may be messing something up in the wrapping part to write each instance of WorkSet. You need to wrap the WorkSet instances because they will not be annotated with @XmlRootElement and JAXB will otherwise refuse to marshal the objects.

这篇关于JAXB 分段编组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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