Java tutorial
//package com.java2s; /******************************************************************************* * Copyright (c) 2015 CA. All rights reserved. * * This source file is licensed under the terms of the Eclipse Public License 1.0 * For the full text of the EPL please see https://www.eclipse.org/legal/epl-v10.html *******************************************************************************/ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import java.io.IOException; import java.io.StringReader; public class Main { /** * Reads an xml string into XML Document. * * @param xmlStr String containing xml * @return xml Document * @throws Exception */ public static Document readXmlString(String xmlStr) throws Exception { Document doc = null; try { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(false); DocumentBuilder builder = domFactory.newDocumentBuilder(); InputSource inStream = new InputSource(); inStream.setCharacterStream(new StringReader(xmlStr)); doc = builder.parse(inStream); } catch (ParserConfigurationException e) { throw new Exception(e.getMessage(), e); } catch (SAXException e) { throw new Exception(e.getMessage(), e); } catch (IOException e) { throw new Exception(e.getMessage(), e); } catch (Exception e) { throw new Exception(e.getMessage(), e); } return doc; } }