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)".

这种方便方法是否已经存在于Java或公共库中?它看起来冗长得可笑,甚至在这个版本中所有的异常都在一个“catch (Exception e)”下崩溃。

2 个解决方案

#1


0

Try this:

试试这个:

DOMSource domSource = new DOMSource(input);
StreamResult resultStream = new StreamResult(output);
TransformerFactory transformFactory = TransformerFactory.newInstance();
try {
    serializer.transform(domSource, resultStream);
} catch (javax.xml.transform.TransformerException e) {
}

更多相关文章

  1. Spring与HSQLDB最新的2.4.0版本无法正常工作
  2. java中多线程安全问题产生&解决方案——同步方法

随机推荐

  1. golang是多线程模式吗?
  2. Go如何使用websocket实现弹幕功能
  3. golang是单进程的吗?
  4. golang如何释放map内存?
  5. 代码详解使用Go基于WebSocket构建视频直
  6. golang如何处理输入?
  7. PHP语法和Go语法有什么差异?对比介绍
  8. golang如何调用函数?
  9. golang是面向对象吗?
  10. 用Go实现字符串的逆序