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

vic*_*107 4 java xml bytearray

我有一个Document(org.w3c.dom.Document),我将这个文件转换为byte数组:

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;
}
Run Code Online (Sandbox Code Playgroud)

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

private Document obtenerDocumentDeByte(byte[] documentoXml) throws Exception {
        ...
}
Run Code Online (Sandbox Code Playgroud)

任何的想法?

Thansks!

har*_*run 16

以下应该有效.

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