Java 中的 XML 输出——DOMImplementationLS 有什么用?
我只需要编写以下愚蠢的类以避免发疯:
import java.io.OutputStream;
import org.w3c.dom.Document;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;
public final class XMLSerializer {
public static final void writeDocument(Document input, OutputStream output) {
try {
DOMImplementationLS ls =
(DOMImplementationLS) DOMImplementationRegistry
.newInstance().getDOMImplementation("LS");
LSSerializer ser = ls.createLSSerializer();
LSOutput out = ls.createLSOutput();
out.setByteStream(output);
ser.write(input, out);
} catch (Exception e) { // DIAF Java
throw new RuntimeException(e);
}
}
}
这种便捷方法是否已存在于 Java 或公共库中? 它看起来冗长得可笑,甚至是所有异常都折叠在“catch (Exception e)”下的版本。
I just had to write the following stupid class to avoid going insane:
import java.io.OutputStream;
import org.w3c.dom.Document;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;
public final class XMLSerializer {
public static final void writeDocument(Document input, OutputStream output) {
try {
DOMImplementationLS ls =
(DOMImplementationLS) DOMImplementationRegistry
.newInstance().getDOMImplementation("LS");
LSSerializer ser = ls.createLSSerializer();
LSOutput out = ls.createLSOutput();
out.setByteStream(output);
ser.write(input, out);
} catch (Exception e) { // DIAF Java
throw new RuntimeException(e);
}
}
}
Does this convenience method already exist in Java or a common library? It seems ridiculously long-winded, and that's even the version where all exceptions are collapsed under a "catch (Exception e)".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这个:
Try this:
尝试 dom4j。 它是一个优秀的 XML 库。
Try dom4j. It is an excellent XML library.