Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 {
        ...
}

Any idea?

Thansks!!!

like image 887
victorpacheco3107 Avatar asked Jan 16 '14 15:01

victorpacheco3107


1 Answers

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));
}
like image 173
harun Avatar answered Sep 28 '22 14:09

harun