本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zxcvbnmluton/archive/2010/04/26/5528747.aspx
二 SAX 用SAX生成xml并换行缩进
package xml;
import java.io.FileOutputStream;
import java.io.StringWriter;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
public class XMLHandler {
public String createXMLFile() {
String xmlStr = null;
try {
Result resultXml = new StreamResult(new FileOutputStream("E://cities.xml"));
StringWriter writerStr = new StringWriter();
SAXTransformerFactory sff = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler th = sff.newTransformerHandler();
Transformer transformer = th.getTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
th.setResult(resultXml);
th.startDocument();
AttributesImpl attr = new AttributesImpl();
th.startElement("", "", "country", attr);
th.startElement("", "", "china", attr);
th.startElement("", "", "city", attr);
String bj = "Beijing";
th.characters(bj.toCharArray(), 0, bj.length());
th.endElement("", "", "city");
th.startElement("", "", "city", attr);
String sh = "Shanghai";
th.characters(sh.toCharArray(), 0, sh.length());
th.endElement("", "", "city");
th.endElement("", "", "china");
th.endElement("", "", "country");
th.endDocument();
xmlStr = writerStr.getBuffer().toString();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return xmlStr;
}
public static void main(String args[]) {
XMLHandler xh = new XMLHandler();
xh.createXMLFile();
}
}
用SAX生成的xml如下
<?xml version="1.0" encoding="UTF-8"?><country><china><city>Beijing</city><city>Shanghai</city></china></country>
同样没有换行和缩进
在代码中增加
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
后生成的xml有了换行但仍没有缩进
<?xml version="1.0" encoding="UTF-8"?>
<country>
<china>
<city>Beijing</city>
<city>Shanghai</city>
</china>
</country>
同样按照DOM中的方法加空格
String four = "\n ";
TransformerHandler.characters(four.toCharArray(),0,four.length());
最终SAX代码如下:
package xml;
import java.io.FileOutputStream;
import java.io.StringWriter;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
public class XMLHandler {
public String createXMLFile() {
String xmlStr = null;
try {
Result resultXml = new StreamResult(new FileOutputStream("E://cities.xml"));
StringWriter writerStr = new StringWriter();
SAXTransformerFactory sff = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler th = sff.newTransformerHandler();
Transformer transformer = th.getTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
th.setResult(resultXml);
th.startDocument();
String four = "\n ";
String eight = "\n ";
AttributesImpl attr = new AttributesImpl();
th.startElement("", "", "country", attr);
th.characters(four.toCharArray(),0,four.length());
th.startElement("", "", "china", attr);
th.characters(eight.toCharArray(),0,eight.length());
th.startElement("", "", "city", attr);
String bj = "Beijing";
th.characters(bj.toCharArray(), 0, bj.length());
th.endElement("", "", "city");
th.characters(eight.toCharArray(),0,eight.length());
th.startElement("", "", "city", attr);
String sh = "Shanghai";
th.characters(sh.toCharArray(), 0, sh.length());
th.endElement("", "", "city");
th.characters(four.toCharArray(),0,four.length());
th.endElement("", "", "china");
th.endElement("", "", "country");
th.endDocument();
xmlStr = writerStr.getBuffer().toString();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return xmlStr;
}
public static void main(String args[]) {
XMLHandler xh = new XMLHandler();
xh.createXMLFile();
}
}
生成的XML
<?xml version="1.0" encoding="UTF-8"?>
<country>
<china>
<city>Beijing</city>
<city>Shanghai</city>
</china>
</country>