對xml的處理_Dom4j入門_Dom4j與w3c之間的Document相互轉換
阿新 • • 發佈:2019-01-07
以下介紹Dom4j的環境是:windowsXP、eclipse3.5.0、jdk1.6、Dom4j1.6.1.jar
Dom4j1.6.1.jar架包可以在本人的資源中下載,歡迎下載使用
/**
* org.w3c.dom.Document -> org.dom4j.Document
* @param doc Document(org.w3c.dom.Document)
* @return Document
*/
public static org.dom4j.Document zhuandom4j(org.w3c.dom.Document doc) throws Exception {
if(doc == null){
return (null);
}
org.dom4j.io.DOMReader xmlReader = new org.dom4j.io.DOMReader();
return (xmlReader.read(doc));
}
/**
* org.dom4j.Document -> org.w3c.dom.Document
* @throws Exception
* @return Document
*/
public static org.w3c.dom.Document zhuanw3c(org.dom4j.Document doc) throws Exception {
if(doc == null){
return (null);
}
java.io.StringReader reader = new java.io.StringReader(doc.asXML());
org.xml.sax.InputSource source = new org.xml.sax.InputSource(reader);
javax.xml.parsers.DocumentBuilderFactory documentBuilderFactory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
javax.xml.parsers.DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
return (documentBuilder.parse(source));
}