JavaRanch Home    
This page:         last edited 25 October 2007         What's Changed?         Edit

Byte Array To Document   

Yet another common question is how one parses a byte array of XML into a DOM Document object. Here's a method that does just that:

import java.i
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;

public class Util {

    public static Document bytesToXml(byte[] xml) 
            throws SAXExceptionParserConfigurationExceptionIOException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.parse(new ByteArrayInputStream(xml));

XmlFaq CategoryCodeSamples

JavaRanchContact us — Copyright © 1998-2014 Paul Wheaton