JavaRanch Home    
This page:         last edited 17 March 2007         What's Changed?         Edit

String To Document   

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

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

public class Util {

    public static Document stringToDom(String xmlSource) 
            throws SAXExceptionParserConfigurationExceptionIOException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.parse(new InputSource(new StringReader(xmlSource)));

XmlFaq CategoryCodeSamples

JavaRanchContact us — Copyright © 1998-2014 Paul Wheaton