使用SAX生成xml简易文件详细步骤如下
package sax;
import java.util.ArrayList;
import java.util.List;
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 SaxGreatXml {
public static void main(String[] args) {
//创建两本书,并存入list中
Book b1 = new Book();
b1.setId("01");
b1.setName("活着");
b1.setAuthor("余华");
b1.setYear("2000");
b1.setPrice("99");
Book b2 = new Book();
b2.setId("02");
b2.setName("雪落香杉树");
b2.setAuthor("福克斯");
b2.setPrice("77");
b2.setLanguage("English");
List bookList = new ArrayList();
bookList.add(b1);
bookList.add(b2);
//创建SaxTransformerFactory对象
SAXTransformerFactory stf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
try {
//通过SaxTransformerFactory对象创建一个TransformerHandler对象
TransformerHandler th = stf.newTransformerHandler();
//通过TransformerHandler对象创建Transformer对象,以便指定xml文件格式
Transformer tf = th.getTransformer();
//设置格式(换行)
tf.setOutputProperty(OutputKeys.INDENT, "yes");
//创建result对象
Result result = new StreamResult("book.xml");
//调用TransformerHandler对象的SetResult方法,使之与TransformerHandler对象相关联
th.setResult(result);
//利用TransformerHandler对象对文件进行编写
//打开Document
th.startDocument();
//创建Attribute类型对象,方便写入属性
AttributesImpl attr = new AttributesImpl();
//插入根节点
th.startElement("", "", "bookstore", attr);
//插入子节点
for(Book book : bookList){
//清空属性
attr.clear();
//添加属性
attr.addAttribute("", "", "id", "",book.getId());
//插入book结点
th.startElement("", "", "book", attr);
//插入name子节点
if(book.getName()!=null && !book.getName().trim().equals("")){
attr.clear();
th.startElement("", "", "name", attr);
//添加结点内容
th.characters(book.getName().toCharArray(), 0, book.getName().length());
th.endElement("", "", "name");
}
// 创建year子节点
if (book.getYear() != null && !book.getYear().trim().equals("")) {
attr.clear();
th.startElement("", "", "year", attr);
th.characters(book.getYear().toCharArray(), 0, book
.getYear().length());
th.endElement("", "", "year");
}
// 创建author子节点
if (book.getAuthor() != null && !book.getAuthor().trim().equals("")) {
attr.clear();
th.startElement("", "", "author", attr);
th.characters(book.getAuthor().toCharArray(), 0, book
.getAuthor().length());
th.endElement("", "", "author");
}
// 创建price子节点
if (book.getPrice() != null && !book.getPrice().trim().equals("")) {
attr.clear();
th.startElement("", "", "price", attr);
th.characters(book.getPrice().toCharArray(), 0, book
.getPrice().length());
th.endElement("", "", "price");
}
// 创建language子节点
if (book.getLanguage() != null && !book.getLanguage().trim().equals("")) {
attr.clear();
th.startElement("", "", "language", attr);
th.characters(book.getLanguage().toCharArray(), 0, book
.getLanguage().length());
th.endElement("", "", "language");
}
th.endElement("", "", "book");
}
th.endElement("", "", "bookstore");
th.endDocument();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
bookList = new ArrayList();
bookList.add(b1);
bookList.add(b2);
//创建SaxTransformerFactory对象
SAXTransformerFactory stf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
try {
//通过SaxTransformerFactory对象创建一个TransformerHandler对象
TransformerHandler th = stf.newTransformerHandler();
//通过TransformerHandler对象创建Transformer对象,以便指定xml文件格式
Transformer tf = th.getTransformer();
//设置格式(换行)
tf.setOutputProperty(OutputKeys.INDENT, "yes");
//创建result对象
Result result = new StreamResult("book.xml");
//调用TransformerHandler对象的SetResult方法,使之与TransformerHandler对象相关联
th.setResult(result);
//利用TransformerHandler对象对文件进行编写
//打开Document
th.startDocument();
//创建Attribute类型对象,方便写入属性
AttributesImpl attr = new AttributesImpl();
//插入根节点
th.startElement("", "", "bookstore", attr);
//插入子节点
for(Book book : bookList){
//清空属性
attr.clear();
//添加属性
attr.addAttribute("", "", "id", "",book.getId());
//插入book结点
th.startElement("", "", "book", attr);
//插入name子节点
if(book.getName()!=null && !book.getName().trim().equals("")){
attr.clear();
th.startElement("", "", "name", attr);
//添加结点内容
th.characters(book.getName().toCharArray(), 0, book.getName().length());
th.endElement("", "", "name");
}
// 创建year子节点
if (book.getYear() != null && !book.getYear().trim().equals("")) {
attr.clear();
th.startElement("", "", "year", attr);
th.characters(book.getYear().toCharArray(), 0, book
.getYear().length());
th.endElement("", "", "year");
}
// 创建author子节点
if (book.getAuthor() != null && !book.getAuthor().trim().equals("")) {
attr.clear();
th.startElement("", "", "author", attr);
th.characters(book.getAuthor().toCharArray(), 0, book
.getAuthor().length());
th.endElement("", "", "author");
}
// 创建price子节点
if (book.getPrice() != null && !book.getPrice().trim().equals("")) {
attr.clear();
th.startElement("", "", "price", attr);
th.characters(book.getPrice().toCharArray(), 0, book
.getPrice().length());
th.endElement("", "", "price");
}
// 创建language子节点
if (book.getLanguage() != null && !book.getLanguage().trim().equals("")) {
attr.clear();
th.startElement("", "", "language", attr);
th.characters(book.getLanguage().toCharArray(), 0, book
.getLanguage().length());
th.endElement("", "", "language");
}
th.endElement("", "", "book");
}
th.endElement("", "", "bookstore");
th.endDocument();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
结果如下
<?xml version="1.0" encoding="UTF-8"?><bookstore>
<book id="01">
<name>活着</name>
<year>2000</year>
<author>余华</author>
<price>99</price>
</book>
<book id="02">
<name>雪落香杉树</name>
<author>福克斯</author>
<price>77</price>
<language>English</language>
</book>
</bookstore>