JavaRanch Home
 
Front Page FAQs Ranchers Categories Recent Changes To Do Upload

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 java.io.*;
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
Front Page FAQs Ranchers Categories Recent Changes To Do Upload
Last Edited: 17 March 2007 What's Changed?
 
Copyright © 1998-2008 Paul Wheaton | Home | About Us | Privacy | Register