Java tutorial
//package com.java2s; //License from project: Open Source License import java.io.*; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.xml.sax.*; import org.w3c.dom.Document; public class Main { public static Document getDocumentFromString(String xml) { Document retVal = null; try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); retVal = db.parse(getInputSourceFromString(xml)); } catch (Exception ex) { ex.printStackTrace(); } return retVal; } /** * Forms an <code>InputSource</code> for parsing XML documents * from a string. Returns <code>null</code> if any of the exceptions * are thrown. * * @param xml String with the XML document. * @return An <code>InputSource</code> representation. */ public static InputSource getInputSourceFromString(String xml) { InputSource retVal = null; try { retVal = new InputSource(new StringReader(xml)); } catch (Exception ex) { } return retVal; } }