Java tutorial
//package com.java2s; //License from project: Apache License 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 { /** * Convert XML string to a XML DOM document * * @param strXML * XML * @return XML DOM document * @throws Exception * in error case */ public static Document xmlStringToDOMDocument(String strXML) throws Exception { if (strXML == null) { throw new RuntimeException("No XML input given(null)!"); } StringReader reader = null; Document doc = null; try { reader = new StringReader(strXML); InputSource inputSource = new InputSource(reader); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); doc = db.parse(inputSource); doc.getDocumentElement().normalize(); } catch (Exception e) { // Logger.XMLEval.logState("Parsing of XML input failed: " + e.getMessage(), LogLevel.Error); throw e; } finally { if (reader != null) { reader.close(); reader = null; } } return doc; } }