Java 在解组期间使用 JAXB 格式化 XML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1074069/
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-08-11 23:10:29  来源:igfitidea点击:

Format XML with JAXB during unmarshal

javaxmljaxbpretty-print

提问by Tobias

I want to format a XML document during unmarshal with JAXB. Unmarshal looks like:

我想在使用 JAXB 解组期间格式化 XML 文档。解组看起来像:

Unmarshaller u = createAndsetUpUnmarshaller(enableValidation, evtHandler, clazz);
return u.unmarshal(new ByteArrayInputStream(stringSource.getBytes()));

While marshaling one can format the code via:

编组时可以通过以下方式格式化代码:

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

But this isn′t possible for the unmarchal process... Any idea how I can format the XML string with JAXB during (or after) unmarshal process?

但这对于解组过程是不可能的......知道如何在解组过程中(或之后)使用 JAXB 格式化 XML 字符串?

BTW: I read some posts here about pretty print, but I want to do it with JAXB!

顺便说一句:我在这里阅读了一些关于漂亮打印的帖子,但我想用 JAXB 来做!

采纳答案by subes

it is logically senseless to format the xml code while unmarshalling it?

在解组它的同时格式化 xml 代码在逻辑上是没有意义的吗?

回答by Isuru Sampath

I think there is no pretty print for Unmarshaller because the result of the JAXB unmarshaller is not an XML but a Java object. If you want to pretty print the resulting unmarshalled object better override the toString() method of the jaxb generated object. (This will be a messy solution since each time you generate the JAX binding classes you will haveto introduce the toString() method yourself.

我认为 Unmarshaller 没有漂亮的打印,因为 JAXB unmarshaller 的结果不是 XML 而是 Java 对象。如果你想漂亮地打印生成的解组对象,最好覆盖 jaxb 生成对象的 toString() 方法。(这将是一个混乱的解决方案,因为每次生成 JAX 绑定类时,您都必须自己引入 toString() 方法。

Hmmm... I hope the future versions of JAXB will have a solution to this shortcoming, since it is important for logging, etc.

嗯...我希望 JAXB 的未来版本能够解决这个缺点,因为它对日志记录等很重要。

回答by Tony Nassar

One way to do this, if you insist, is to use an XSLT Transformer, such as Saxon's, that supports "teeing," i.e. lets you transform a Source to two Result objects. I don't know why you call String#getBytes(); you should be creating a StringReader and pulling from that. The two destinations for your "tee" would be the "identity transform" (the default if you call TransformerFactory#newTransformer()) and the other would be JAXBResult.

如果您坚持,一种方法是使用 XSLT 转换器,例如 Saxon 转换器,它支持“开球”,即将一个 Source 转换为两个 Result 对象。我不知道你为什么叫 String#getBytes(); 您应该创建一个 StringReader 并从中提取。“tee”的两个目的地将是“身份转换”(如果您调用 TransformerFactory#newTransformer(),则为默认值),另一个将是 JAXBResult。

回答by Justin Rowe

If you want to log formatted XML corresponding to the XML that you just unmarshalled, you can simply remarshal the unmarshalled object back to XML, using the property you specified, ie.

如果您想记录与您刚刚解组的 XML 相对应的格式化 XML,您可以简单地使用您指定的属性将解组的对象重新编组回 XML,即。

/**
 * Marshall input object to a formatted XML String
 */
protected <T> String marshal(T input) throws JAXBException {
    StringWriter writer = new StringWriter();

    JAXBContext jc = JAXBContext.newInstance(input.getClass());
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(input, writer);
    return writer.toString();
}

On the other hand, if all you want to do is reformat the XML, you probably should be using JAXP instead of JAXB.

另一方面,如果您只想重新格式化 XML,您可能应该使用 JAXP 而不是 JAXB。