我正在开发一个使用不同组件创建 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 验证。