Java / JAXB:如何将类序列化为格式良好的xml文档?

时间:2022-01-22 07:18:55

I love JAXB. It has made my work so much easier. I do have one minor gripe: The XML it outputs is always dumped onto a single line. I call it a minor gripe because typically I don't really care what's in the XML and no user should either, and if they stumble upon and brutalize the contents I generally implement fail safes to revert back to install-time defaults. However, some times I just do really kind of want to look at whats in there, to make sure things are running right, and sometimes just to see what it's doing and how it's doing it.

我喜欢JAXB。它使我的工作变得更加容易。我确实有一个小抱怨:它输出的XML总是被转储到一行。我称之为一个小小的抱怨,因为通常我并不关心XML中的内容,也没有用户应该这样做,如果他们偶然发现并残酷化了内容,我通常会将故障保险箱恢复为安装时默认值。然而,有些时候我真的很想看看那里的什么,以确保事情正常运行,有时只是为了看看它在做什么以及它是如何做的。

Is it possible to get JAXB to serialize a class to a well-formated XML document? Like, rather than

是否有可能让JAXB将类序列化为格式良好的XML文档?喜欢,而不是

<Class><Object>value</Object><Object2>value</Object2></Class>

something like

<Class>
    <Object>
        value
    </Object>
</Class>

Is that possible?

那可能吗?

1 个解决方案

#1


4  

You need to set the following formatting option to marshaller:

您需要为marshaller设置以下格式化选项:

marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );

Documentation:

#1


4  

You need to set the following formatting option to marshaller:

您需要为marshaller设置以下格式化选项:

marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );

Documentation: