1. 程式人生 > >將字串轉為document

將字串轉為document

public static Document string2doc(String xml) throws ParserConfigurationException, UnsupportedEncodingException, SAXException, IOException{ if (db == null) { //首先得到:得到 DOM 解析器的工廠例項  ; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); //然後從 DOM 工廠獲得 DOM 解析器
db = dbf.newDocumentBuilder(); } //將XML字串轉化為輸入流,通過DOM 解析器轉化為DOM文件。 Document doc = db.parse(new java.io.ByteArrayInputStream(xml.getBytes("utf-8"))); doc.normalize(); return doc; }