JAXB碎片编组 [英] JAXB Fragmented Marshalling

查看:98
本文介绍了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>

< Import> < WorkSets> 元素基本上只是包含大量< WorkSet> &的容器元素。 <工作> 元素。我目前正试图在< WorkSet> 进行编组。

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. 是有可能最初编组< Import> < WorkSets> 元素,然后从那时起编组< WorkSet> 元素并将输出括在< Import>< WorkSets> 标记中?

  2. 当我在WorkSet级别编组时,它将 xmlns ='http://namespace.com'属性附加到WorkSet tag,有没有一种方法可以在没有将namespace属性附加到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实例流式化为marshal。

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并通过将它们包装在JAXBElement中来封送WorkSet对象。我手边没有经过测试的示例代码,所以这里是粗略的代码片段,可以让你进入写入轨道:

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天全站免登陆