Here you can find the source of stringToDocument(String doc)
Parameter | Description |
---|---|
doc | the string to parse |
Parameter | Description |
---|---|
SAXException | if the string did not contain valid html. |
public static Document stringToDocument(String doc) throws SAXException
//package com.java2s; //License from project: Apache License import org.w3c.dom.Document; import org.xml.sax.SAXException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; public class Main { /**/*from ww w . j a v a 2 s. co m*/ * Simple method to parse a string into a w3c Document * @param doc the string to parse * @return the document * @throws SAXException if the string did not contain valid html. */ public static Document stringToDocument(String doc) throws SAXException { InputStream in = new ByteArrayInputStream(doc.getBytes()); try { return getDOCUMENT_BUILDER().parse(in); } catch (IOException e) { throw new Error("Problem reading a string, should never happen", e); } } public static DocumentBuilder getDOCUMENT_BUILDER() { try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); return documentBuilderFactory.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new Error("Error initialising default document builder", e); } } }