具体案例如下:
package com.sinosoft.SAXToXml;
import org.xml.sax.helpers.AttributesImpl;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
public class TestDemo01 {
public static void main(String[] args) throws Exception {
//1.创建SAXTransformerFactory实例
SAXTransformerFactory saxTransformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
//2.创建TransformerHandler实例
TransformerHandler transformerHandler = saxTransformerFactory.newTransformerHandler();
//3.创建Transformer实例
Transformer transformer = transformerHandler.getTransformer();
//4.设置输出的xml的属性:ENCODING为编码方式,INDENT是确保输出的xml文件能自动换行
transformer.setOutputProperty(OutputKeys.ENCODING,"utf-8");
transformer.setOutputProperty(OutputKeys.INDENT