XMLIO.java 文件源码

java
阅读 33 收藏 0 点赞 0 评论 0

项目:automation_engine 作者:
/**
 * Returns a textual representation of an XML Object.
 * 
 * @param doc   XML Dom Document
 * @return  String containing a textual representation of the object
 */
public static String asString(Document doc) {
    try {
        DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
        LSSerializer lsSerializer = domImplementation.createLSSerializer();
        LSOutput lsOutput =  domImplementation.createLSOutput();
        lsOutput.setEncoding("UTF-8");
        return lsSerializer.writeToString(doc);
    } catch (Exception e) {
        e.printStackTrace();
        try {
            DOMSource domSource = new DOMSource(doc);
            StringWriter writer = new StringWriter();
            StreamResult result = new StreamResult(writer);
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            transformer.transform(domSource, result);
            return writer.toString();
        } catch(Exception ex) {
            logger.error(ex);
            return StackTrace.asString(ex);
        }
    }

}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号