如何将数组字节转换为org.w3c.dom.Document [英] how to convert array byte to org.w3c.dom.Document

查看:133
本文介绍了如何将数组字节转换为org.w3c.dom.Document的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Document (org.w3c.dom.Document), I convert this document to array of 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;
}

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

I need convert the array of byte to document newly:

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

任何想法?

Thansks !!!

Thansks!!!

推荐答案

以下情况应该有效。

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

这篇关于如何将数组字节转换为org.w3c.dom.Document的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆