创建XML的最快,最有效的方法 [英] Fastest and most efficient way to create XML
问题描述
用Java创建XML文档的最快,最有效的方法是什么?那里有很多库(woodstox,xom,xstream ......),只是想知道是否有人有任何输入。我应该采用代码生成方法(因为xml架构是众所周知的)?或者在运行时采用反射方法?
What is the fastest and most efficient way to create XML documents in Java? There is a plethora of libraries out there (woodstox, xom, xstream...), just wondering if any one has any input. Should I go with code generation approach (since xml schema is well known)? Or reflection approach at run-time?
使用附加信息进行编辑:
- 嗯定义的XML Schema可用且很少更改
- 要求是将java对象转换为XML,而不是反之亦然
- 成千上万的Java对象转换为XML每秒
- 代码生成,代码复杂性,配置,维护等是性能提升的第二位。
- Well defined XML Schema is available and rarely changes
- Requirement is to convert a java object to XML, and not vice versa
- Thousands of java objects to XML per second
- Code generation, code complexity, configuration, maintenance etc. is second to higher performance.
推荐答案
如果我要创建非常简单的XML内容,我会坚持使用JDK api,不会引入任何第三方依赖。
If I was to create a very simple XML content, I would stick to the JDK api only, introducing no third party dependencies.
因此,对于简单的XML,如果我将XML文件映射到Java类(反之亦然),我会去 JAXB 。 查看本教程,了解它是多么容易。
So for simple XML and if I was to map XML file to Java classes (or vice-versa), I would go for JAXB. See this tutorial to see how easy it is.
现在。
如果我要使用常量方案创建一些更复杂的XML输出,我会使用一些模板引擎,也许是 Freemarker 。 Thymeleaf 也很不错。
If I was to create some more sophisticated XML output with constant scheme, I would use some templating engine, Freemarker perhaps. Thymeleaf looks nice as well.
最后。
如果我要非常有效地创建巨大的XML文件,我会使用 SAX解析器。
If I was to create huge XML files very effectively, I would use SAX parser.
我希望您现在明白,您有很多可能性 - 根据您的需求选择最佳匹配:)
I hope you understand now, that you have plenty of possibilities - choose the best match for your needs :)
玩得开心!
这篇关于创建XML的最快,最有效的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!