Here you can find the source of getDocumentFromString(String text)
Parameter | Description |
---|---|
text | the document content to be parsed. |
public static Document getDocumentFromString(String text)
//package com.java2s; /*//w w w . j ava 2 s .co m Copyright (C) 2007-2011 BlueXML - www.bluexml.com This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.io.ByteArrayInputStream; import java.io.InputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; public class Main { private static DocumentBuilder documentBuilder = null; /** * Returns a new document built from the input stream. * * @param text * the document content to be parsed. * @return * @since 1.0.2 */ public static Document getDocumentFromString(String text) { Document document; try { DocumentBuilder docBuilder = getDocumentBuilder(); InputStream is = new ByteArrayInputStream(text.getBytes("UTF-8")); document = docBuilder.parse(is); } catch (Exception e) { e.printStackTrace(); return null; } return document; } /** * Gets a namespace aware non-validating DocumentBuilder object. If non existent, the object is * created. * * @return */ public static DocumentBuilder getDocumentBuilder() { if (documentBuilder == null) { try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setNamespaceAware(true); // @since 1.0.2 docBuilderFactory.setValidating(false); // @since 1.0.2 documentBuilder = docBuilderFactory.newDocumentBuilder(); } catch (ParserConfigurationException e) { e.printStackTrace(); return null; } } return documentBuilder; } }