本文实例为大家分享了Java操作XML工具类的具体代码,供大家参考,具体内容如下
一、代码
public class XmlUtil {
/**
* 将XML文件输出到指定的路径
*
* @param doc
* @param fileName
* @throws Exception
*/
public static void outputXml(Document doc, String fileName)
throws Exception {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
DOMSource source = new DOMSource(doc);
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
StreamResult result = new StreamResult(pw);
transformer.transform(source, result);
System.out.println("生成XML文件成功!");
}
/**