java Jaxb 可以在没有根元素的情况下封送子元素吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11823352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:30:30  来源:igfitidea点击:

Can Jaxb marshal child elements without the root element?

javaxmljaxbmarshalling

提问by Meny Issakov

I'm not sure if the following question is possible with jaxb, but I'll ask anyway.

我不确定 jaxb 是否可以解决以下问题,但我还是会问。

In a certain project, we're using jaxb with a defined schema to create the next structure of xml file.

在某个项目中,我们使用带有定义模式的 jaxb 来创建 xml 文件的下一个结构。

<aaa>
     <bbb>
        more inner children here
     </bbb>
     <bbb>
        more inner children here
     </bbb>
</aaa>

We're also using the automatic class generating of jaxb which creates the classes: aaa and bbb, where aaa was generated as the @XmlRootElement.

我们还使用 jaxb 的自动类生成,它创建类:aaa 和 bbb,其中 aaa 生成为 @XmlRootElement。

We now want to use the same schema in a new project, that will be also compatible with the previous project. What I would like to do, is to use the same jaxb generated classes, without performing any changes in the schema in order to marshal only a single bbb object into xml.

我们现在想在一个新项目中使用相同的模式,这也将与以前的项目兼容。我想要做的是使用相同的 jaxb 生成的类,而不在架构中执行任何更改,以便仅将单个 bbb 对象编组到 xml 中。

JAXBContext jc = JAXBContext.newInstance("generated");
Marshaller marshaller = jc.createMarshaller();
marshaller.marshal(bbb, writer);

So we would get the next result:

所以我们会得到下一个结果:

 <bbb>
    <inner child1/>
    <inner child2/>
    ...
 </bbb>

I'm currently not able to do so as the marshaller yells that I do not have a @XmlRootElement defined.

我目前无法这样做,因为编组员大喊我没有定义 @XmlRootElement。

We're actually trying to avoid the case of separating the schema into 2 schemas, one of only bbb and the other where aaa imports bbb.

我们实际上是在尝试避免将模式分成 2 个模式的情况,其中一个只有 bbb,另一个是 aaa 导入 bbb。

Thanks in advance!

提前致谢!

回答by Lazar Lazarov

I am maybe late with 3 years but have you ever tried something like that :

我可能迟到了 3 年,但你有没有尝试过这样的事情:

public static String marshal(Bbb bbb) throws JAXBException {
    StringWriter stringWriter = new StringWriter();

    JAXBContext jaxbContext = JAXBContext.newInstance(Bbb.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    // format the XML output
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    QName qName = new QName("com.yourModel.bbb", "bbb");
    JAXBElement<Bbb> root = new JAXBElement<Bbb>(qName, Bbb.class, bbb);

    jaxbMarshaller.marshal(root, stringWriter);

    String result = stringWriter.toString();
    LOGGER.info(result);
    return result;
}

Here is the article I use when I have to marshal/unmarshal without rootElement : http://www.source4code.info/2013/07/jaxb-marshal-unmarshal-with-missing.html

这是我在没有 rootElement 的情况下必须编组/解组时使用的文章:http: //www.source4code.info/2013/07/jaxb-marshal-unmarshal-with-missing.html

It works pretty fine for me. I am writing this response for other lost souls searching for answers .

它对我来说很好用。我正在为其他寻找答案的迷失灵魂写这篇回应。

All the best : )

一切顺利 : )

回答by cheb1k4

I am maybe late with 5 years :) but have you ever tried something like that :

我可能迟到了 5 年 :) 但你有没有尝试过这样的事情:

StringWriter stringWriter = new StringWriter();
JAXB.marshal(bbb, stringWriter);
String bbbString = stringWriter.toString();