Java 如何将数组字节转换为 org.w3c.dom.Document

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21165871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:03:48  来源:igfitidea点击:

how to convert array byte to org.w3c.dom.Document

javaxmlbytearray

提问by victorpacheco3107

I have a Document (org.w3c.dom.Document), I convert this document to array of byte:

我有一个文档(org.w3c.dom.Document),我将此文档转换为字节数组:

private byte[] obtenerBytesDeDocument(Document documentoXml) throws Exception {
    Source source = new DOMSource( documentoXml );
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Result result = new StreamResult(out);
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.transform(source, result);
    byte[] butesXml =  out.toByteArray();
    return butesXml;
}

I need convert the array of byte to document newly:

我需要将字节数组转换为新的文档:

private Document obtenerDocumentDeByte(byte[] documentoXml) throws Exception {
        ...
}

Any idea?

任何的想法?

Thansks!!!

谢谢!!!

采纳答案by harun

The following should work.

以下应该工作。

private Document obtenerDocumentDeByte(byte[] documentoXml) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(new ByteArrayInputStream(documentoXml));
}