JAXB马歇尔缩进

JAXB马歇尔缩进,第1张

JAXB马歇尔缩进

可以通过将javax Transformer应用于输出来解决此烦人的问题。

import javax.xml.transform.*;import javax.xml.transform.dom.*;import javax.xml.transform.stream.StreamResult;Object jaxbElement = // The object you want to marshall using jaxb.JAXBContext context = JAXBContext.newInstance(jaxbElement.getClass());Marshaller marshaller = context.createMarshaller();OutputStream out = // Here your destination, FileOutStream, ByteOutStream etcDOMResult domResult = new DOMResult();marshaller.marshal(jaxbElement, domResult);Transformer transformer = TransformerFactory.newInstance().newTransformer();transformer.setOutputProperty(OutputKeys.INDENT, "yes");transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");transformer.transform(new DOMSource(domResult.getNode()), new StreamResult(out));


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5490663.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存