JAXB 编组器初始化
我需要将 JAXB 对象编组为 xml 格式字符串。我正在使用 SLSB 并挂钩代码来创建 Marshaller
以及 @PostConstruct
带注释的方法中的其他内容。这样每次我都不需要加载架构并创建编组器。
@PostConstruct
注解方法中的代码如下。
JAXBContext jaxbContext = JAXBContext.newInstance(jaxbPackageName);
SchemaFactory factory = SchemaFactory
.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
URL schemaUrl = Thread.currentThread().getContextClassLoader()
.getResource(resourcePath);
schema = factory.newSchema(schemaUrl);
setMarshaller(jaxbContext.createMarshaller());
getMarshaller().setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
Boolean.FALSE);
getMarshaller().setSchema(schema);
getMarshaller().setEventHandler(new DefaultValidationEventHandler());
setUnmarshaller(jaxbContext.createUnmarshaller());
getUnmarshaller().setSchema(schema);
getUnmarshaller().setEventHandler(new DefaultValidationEventHandler());
当客户端代码需要 xml 格式的对象时,以下方法返回相同的结果。
OutputStream outputStream = new ByteArrayOutputStream();
getMarshaller().setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
schemaLocation);
getMarshaller().marshal(document, outputStream);
xmlString = outputStream.toString();
我关心的是,是否有更好的方法来执行相同的操作(每当客户端代码想要获取 JAXB 对象的 xml 格式时,返回相同内容的最快方法是什么?)。
谢谢
I need to Marshal a JAXB object to xml format string. I'm using a SLSB and hook the code to create the Marshaller
and other things in @PostConstruct
annotated method. So that each time I need not load the schema and create the Marshaller.
The code in @PostConstruct
annotated method is as below.
JAXBContext jaxbContext = JAXBContext.newInstance(jaxbPackageName);
SchemaFactory factory = SchemaFactory
.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
URL schemaUrl = Thread.currentThread().getContextClassLoader()
.getResource(resourcePath);
schema = factory.newSchema(schemaUrl);
setMarshaller(jaxbContext.createMarshaller());
getMarshaller().setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
Boolean.FALSE);
getMarshaller().setSchema(schema);
getMarshaller().setEventHandler(new DefaultValidationEventHandler());
setUnmarshaller(jaxbContext.createUnmarshaller());
getUnmarshaller().setSchema(schema);
getUnmarshaller().setEventHandler(new DefaultValidationEventHandler());
And when a client code needs xml format of the object the following method returns the same.
OutputStream outputStream = new ByteArrayOutputStream();
getMarshaller().setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
schemaLocation);
getMarshaller().marshal(document, outputStream);
xmlString = outputStream.toString();
My concern is, is there any better way to do the same (whenever a client code wants to get xml format of a JAXB object, the fastest approach to return the same? ).
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道使用 JAXB 进行编组/解编组的任何其他方式。当我遇到同样的问题时,我检查了CXF代码,也是一样的。
请参阅 JAXB 性能和线程安全
I don't know any other way of marshalling/unmarshalling with JAXB. When I had the same problem, I checked the CXF code and it was the same.
Refer to JAXB Performance and Thread safety
与您的问题没有直接关系,但 ByteArrayOutputStream#toString() 将使用平台的默认字符编码,而不是创建 XML 文档时 JAXB 使用的编码。根据您对 XML 文档的进一步计划,最好将其保留为字节数组,而不是尝试从中生成可能不正确的字符串。
Not directly related to your question, but ByteArrayOutputStream#toString() will use the platform's default character encoding and not the encoding used by JAXB when creating the XML document. Depending on your further plans with the XML document, it might be better to keep it as a byte array instead of trying to make a potentially incorrect String out of it.