public static Document toDocument ( String xml ) { try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory . newInstance ( ) ; documentBuilderFactory . setNamespaceAware ( true ) ; documentBuilderFactory . setIgnoringElementContentWhitespace ( true ) ; documentBuilderFactory . isIgnoringComments ( ) ; documentBuilderFactory . setCoalescing ( true ) ; DocumentBuilder documentBuilder = documentBuilderFactory . newDocumentBuilder ( ) ; InputSource inputSource = new InputSource ( ) ; inputSource . setCharacterStream ( new StringReader ( xml ) ) ; Document document = documentBuilder . parse ( inputSource ) ; removeUnusedNamespaces ( document ) ; return document ; } catch ( ParserConfigurationException e ) { throw new RuntimeException ( e ) ; } catch ( SAXException e ) { throw new RuntimeException ( e ) ; } catch ( IOException e ) { throw new RuntimeException ( e ) ; } }
Creates a Document from XML String