使用 StringTemplate 创建 XML 的性能?
我正在开发一个应用程序,它使用不同的组件来创建 XHTML 文档,我使用 StringTemplate 来创建组件的文档数据,然后将它们组合成一个大文档。这是一个组件的示例:
public class BoxImpl extends AbstractContainerImpl implements Box {
private static final StringTemplate template;
static {
template = new StringTemplate(
"<div id=$id$>$content$</div>");
}
public BoxImpl(String id) {
this.setId(id);
}
@Override
public CharBuffer generate() {
// Get a local instance
StringTemplate template = BoxImpl.template.getInstanceOf();
// Set ID attribute of box
template.setAttribute("id", this.getId());
// Generate view for controls inside this container
CharBuffer inner = this.generateInner();
// Add inner data as content attribute
template.setAttribute("content", inner == null ? "" : inner.array());
// Return the result
return CharBuffer.wrap(BoxImpl.template.toString());
}
}
我的问题是,与 StringTemplate 相比,使用 XML DOM 或 StringBuilder 实现这种文档构建是否更有效?
编辑:我不需要 XML 验证。
I'm developing an application which uses different components to create an XHTML document, I used StringTemplate for creating the document data of the components and then combined them into a one large document. This is an example of a component:
public class BoxImpl extends AbstractContainerImpl implements Box {
private static final StringTemplate template;
static {
template = new StringTemplate(
"<div id=$idgt;$contentlt;/div>");
}
public BoxImpl(String id) {
this.setId(id);
}
@Override
public CharBuffer generate() {
// Get a local instance
StringTemplate template = BoxImpl.template.getInstanceOf();
// Set ID attribute of box
template.setAttribute("id", this.getId());
// Generate view for controls inside this container
CharBuffer inner = this.generateInner();
// Add inner data as content attribute
template.setAttribute("content", inner == null ? "" : inner.array());
// Return the result
return CharBuffer.wrap(BoxImpl.template.toString());
}
}
My question is, is it more efficient to implement this kind of document building using XML DOM or a StringBuilder, compared to the StringTemplate?
EDIT: I do not need XML validation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从性能的角度来看,我很确定 DOM 会比使用 StringTemplate 更糟糕。使用 StringBuilder 可能会更快一些,甚至看起来更干净(如果隐式使用):
最快的方法可能是完全避免创建临时字符串,即直接写入 BufferedOutputWriter 或 PrintWriter。
但一般来说,我建议使用专用的 Stream Writer API 之一来创建 XML 文档。当您将普通字符串直接放入 XML 文档而不注意特殊字符的正确转义时,会出现一些不明显的陷阱。这些 API 通常还提供一种失败高效的实现,击败了大多数简单的方法。此类 API 的示例有:StAX、Apache XMLIO 和 SAX Transformer。
From a perfomance point of view I'm pretty sure DOM would be worse than your usage of StringTemplate. Using StringBuilder might be a bit faster and maybe even cleaner looking (if used implicitly):
The fastest way to do this would probably be to avoid the creation of temporary strings entirely, i.e. to write directly into a BufferedOutputWriter or PrintWriter.
But in general I'd suggest to use one of the dedicated Stream Writer APIs for the creation of XML documents. There are several non obvious pitfalls when you put vanilla strings directly into a XML documents without awareness for proper escaping of special characters. These APIs usually offer also a failry efficient implementation that beats most naive approaches. Examples for such APIs are: StAX, Apache XMLIO and SAX Transformer.