Java tutorial
//package com.java2s; import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.xml.sax.InputSource; public class Main { /** * Uses the current DocumentBuilderFactory to converts a String * representation of XML into a Document. * * @param xml XML serialized as a String * @return Document */ public static Document stringToDocument(String xml) { return stringToDocument(xml, true); } /** * Uses the current DocumentBuilderFactory to converts a String * representation of XML into a Document. * * @param xml XML serialized as a String * @param nameSpaceAware determines whether the constructed Document * is name-space aware * @return Document */ public static Document stringToDocument(String xml, boolean nameSpaceAware) { Document document = null; try { if (xml != null) { StringReader reader = new StringReader(xml); InputSource input = new InputSource(reader); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(nameSpaceAware); factory.setValidating(false); DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.parse(input); } } catch (Exception ex) { ex.printStackTrace(); } return document; } }