使用Sax生成xml文件

来源:互联网 发布:最新网络币 编辑:程序博客网 时间:2024/05/24 02:57

本文介绍如何使用Sax通过实体类生成对应的xml文件

一、实体类如下

public class Book {private String id;private String name;private String author;private String price;private String year;private String edition;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}public String getPrice() {return price;}public void setPrice(String price) {this.price = price;}public String getYear() {return year;}public void setYear(String year) {this.year = year;}public String getEdition() {return edition;}public void setEdition(String edition) {this.edition = edition;}@Overridepublic String toString() {return "Book [id=" + id + ", name=" + name + ", author=" + author + ", price=" + price + ", year=" + year+ ", edition=" + edition + "]";}}

二、数据如下,为三本书的集合

Book [id=156, name=计算机网络, author=谢希仁, price=39, year=2013, edition=null]  Book [id=234, name=计算机操作系统, author=佚名, price=40, year=2013, edition=第四版]  Book [id=367, name=计算机组成原理, author=null, price=35, year=2013, edition=第三版]  

三、此处,我们通过自己编写的一个方法将三本书封装到bookList中,读者不用了解细节,此处只是提供数据源

List<Book> bookList = SaxXmlParser.parseXml();

四、Sax的准备工作

List<Book> bookList = SaxXmlParser.parseXml();// 1、创建SAXTransformerFactory实例SAXTransformerFactory tff = (SAXTransformerFactory) SAXTransformerFactory.newInstance();// 2、创建TransformerHandler,负责生成xml节点TransformerHandler handler = tff.newTransformerHandler();// 3、创建Transformer,要使其生效,一定要放在TransformerHandler.setResult之前Transformer transform = handler.getTransformer();// 4、通过Transformer对象对生成的xml维恩爱你进行配置transform.setOutputProperty(OutputKeys.ENCODING, "UTF-8");transform.setOutputProperty(OutputKeys.ENCODING, "yes");File file = new File("book1.xml");// 5、创建一个Result对象,Result对象相当于对将要生成的xml文件的封装Result reslut = new StreamResult(file);// 6、将Result对象与handler关联handler.setResult(reslut);


五、利用TransformerHandler对象进行xml文件编写,生成xml节点

TransformerHandler有如下几个常用的方法

startDocument,开始生成xml文档

startElement,开始生成节点

characters,填入标签中的文本

endElement,节点结束

endElement,xml文档结束


//利用handler对象进行xml文件编写handler.startDocument();handler.startElement("", "", "bookstore", null);for (Book book : bookList) {// 生成属性的工具类AttributesImpl attr = new AttributesImpl();attr.addAttribute("", "", "id", "", book.getId());handler.startElement("", "", "book", attr);if (book.getName() != null) {handler.startElement("", "", "name", null);handler.characters(book.getName().toCharArray(), 0, book.getName().length());handler.endElement("", "", "name");}if (book.getAuthor() != null) {handler.startElement("", "", "author", null);handler.characters(book.getAuthor().toCharArray(), 0, book.getAuthor().length());handler.endElement("", "", "author");}if (book.getEdition() != null) {handler.startElement("", "", "edition", null);handler.characters(book.getEdition().toCharArray(), 0, book.getEdition().length());handler.endElement("", "", "edition");}if (book.getPrice() != null) {handler.startElement("", "", "price", null);handler.characters(book.getPrice().toCharArray(), 0, book.getPrice().length());handler.endElement("", "", "price");}if (book.getYear() != null) {handler.startElement("", "", "year", null);handler.characters(book.getYear().toCharArray(), 0, book.getYear().length());handler.endElement("", "", "year");}handler.endElement("", "", "book");



六、结果

生成一个book1.xml文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?><bookstore><book id="156"><name>计算机网络</name><author>谢希仁</author><price>39</price><year>2013</year></book><book id="234"><name>计算机操作系统</name><author>佚名</author><edition>第四版</edition><price>40</price><year>2013</year></book><book id="367"><name>计算机组成原理</name><edition>第三版</edition><price>35</price><year>2013</year></book></bookstore>


完成代码如下

public class SaxXmlCreator {public static void main(String[] args) {List<Book> bookList = SaxXmlParser.parseXml();// 1、创建SAXTransformerFactory实例SAXTransformerFactory tff = (SAXTransformerFactory) SAXTransformerFactory.newInstance();try {// 2、创建TransformerHandlerTransformerHandler handler = tff.newTransformerHandler();// 3、创建Transformer,要使其生效,一定要放在TransformerHandler.setResult之前Transformer transform = handler.getTransformer();// 4、通过Transformer对象对生成的xml维恩爱你进行配置transform.setOutputProperty(OutputKeys.ENCODING, "UTF-8");transform.setOutputProperty(OutputKeys.ENCODING, "yes");File file = new File("book1.xml");// 5、创建一个Result对象Result reslut = new StreamResult(file);// 6、将Result对象与handler关联handler.setResult(reslut);// 7、利用handler对象进行xml文件编写handler.startDocument();handler.startElement("", "", "bookstore", null);for (Book book : bookList) {// 生成属性的工具类AttributesImpl attr = new AttributesImpl();attr.addAttribute("", "", "id", "", book.getId());handler.startElement("", "", "book", attr);if (book.getName() != null) {handler.startElement("", "", "name", null);handler.characters(book.getName().toCharArray(), 0, book.getName().length());handler.endElement("", "", "name");}if (book.getAuthor() != null) {handler.startElement("", "", "author", null);handler.characters(book.getAuthor().toCharArray(), 0, book.getAuthor().length());handler.endElement("", "", "author");}if (book.getEdition() != null) {handler.startElement("", "", "edition", null);handler.characters(book.getEdition().toCharArray(), 0, book.getEdition().length());handler.endElement("", "", "edition");}if (book.getPrice() != null) {handler.startElement("", "", "price", null);handler.characters(book.getPrice().toCharArray(), 0, book.getPrice().length());handler.endElement("", "", "price");}if (book.getYear() != null) {handler.startElement("", "", "year", null);handler.characters(book.getYear().toCharArray(), 0, book.getYear().length());handler.endElement("", "", "year");}handler.endElement("", "", "book");}handler.endElement("", "", "bookstore");handler.endDocument();} catch (TransformerConfigurationException | SAXException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}


0 0
原创粉丝点击