import java.io.ByteArrayOutputStream;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.xml.sax.InputSource;
/**
* w3c document 与 dom4j document转化工具类
*
* @author zhang.haifeng
*
*/
public class DocTransformUtil {
// 单例
private static final DocTransformUtil DOC_TRANSFORM_UTIL = new DocTransformUtil();
private DocTransformUtil() {
}
/**
* 获取一个工具类实例
*
* @return
*/
public DocTransformUtil getInstance() {
return DOC_TRANSFORM_UTIL;
}
/**
* org.w3c.dom.Document -> org.dom4j.Document
*
* @param doc
* Document(org.w3c.dom.Document)
* @return Document
*/
public static Document parse(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
*
* @param doc
* Document(org.dom4j.Document)
* @throws Exception
* @return Document
*/
public static org.w3c.dom.Document parse(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));
}
/**
* string -> document(w3c)
*
* @param xmlStr
* @return
* @throws Exception
*/
public static org.w3c.dom.Document stringToW3cDoc(String xmlStr)
throws Exception {
StringReader sr = new StringReader(xmlStr);
InputSource is = new InputSource(sr);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
org.w3c.dom.Document doc = builder.parse(is);
return doc;
}
/**
* string -> document(dom4j)
*
* @param xmlStr
* @return
* @throws DocumentException
*/
public static Document stringToDom4jDoc(String xmlStr)
throws DocumentException {
Document document = DocumentHelper.parseText(xmlStr);
return document;
}
/**
* document(w3c)-> string
*
* @param doc
* @return
* @throws Exception
*/
public static String w3cDocToString(org.w3c.dom.Document doc)
throws Exception {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
t.setOutputProperty("/encoding/", "/GB23121/");// 解决中文问题,试过用GBK不行
ByteArrayOutputStream bos = new ByteArrayOutputStream();
t.transform(new DOMSource(doc), new StreamResult(bos));
String xmlStr = bos.toString();
return xmlStr;
}
/**
* document(dom4j)->string
* @param doc
* @return
*/
public static String dom4jDocToString(Document doc) {
String str = doc.asXML();
return str;
}
}