Java 怎么将org.w3c.dom.document转为xml字符串

2019年7月29日19:15:35Java 怎么将org.w3c.dom.document转为xml字符串已关闭评论 842 780字阅读2分36秒

1.简介

Java 怎么将org.w3c.dom.document转为xml字符串?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


import java.io.StringWriter;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

public class Main {
    
     
    public static String toXML(org.w3c.dom.Document w3cDoc)
            throws ParserConfigurationException, TransformerException {
        final DOMSource source = new DOMSource(w3cDoc);
        final StringWriter writer = new StringWriter();
        final StreamResult result = new StreamResult(writer);
        final Transformer transformer = TransformerFactory.newInstance()
                .newTransformer();
        transformer.transform(source, result);
        return writer.toString();
    }
}

 
编程十万个怎么办