将字符串转为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;
}

猜你喜欢

转载自blog.csdn.net/m0_37488170/article/details/72779290