}
运行结果:
2.SAX
1 import java.io.File;
2 import java.io.FileOutputStream;
4 import javax.xml.transform.OutputKeys;
5 import javax.xml.transform.Result;
6 import javax.xml.transform.Transformer;
7 import javax.xml.transform.sax.SAXTransformerFactory;
8 import javax.xml.transform.sax.TransformerHandler;
9 import javax.xml.transform.stream.StreamResult;
11 import org.xml.sax.helpers.AttributesImpl;
13 public class SaxToXmlDemo {
15 // 生成xml
16 public static void createXml() {
17 // 1、创建一个SAXTransformerFactory类的对象
18 SAXTransformerFactory tff = (SAXTransformerFactory) SAXTransformerFactory
19 .newInstance();
20 try {
21 // 2、通过SAXTransformerFactory创建一个TransformerHandler的对象
22 TransformerHandler handler = tff.newTransformerHandler();
23 // 3、通过handler创建一个Transformer对象
24 Transformer tr = handler.getTransformer();
25 // 4、通过Transformer对象对生成的xml文件进行设置
26 // 设置编码方式
27 tr.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
28 // 设置是否换行
29 tr.setOutputProperty(OutputKeys.INDENT, "yes");
30 // 5、创建一个Result对象
31 File f = new File("book2.xml");
32 // 判断文件是否存在
33 if (!f.exists()) {
34 f.createNewFile();
35 }
36 Result result = new StreamResult(new FileOutputStream(f));
37 // 6、使Result与handler关联
38 handler.setResult(result);
40 // 打开document
41 handler.startDocument();
42 AttributesImpl attr = new AttributesImpl();
43 handler.startElement("", "", "bookstore", attr);
44 // attr.clear();
46 for (int i = 1; i <= 3; i++) {
47 attr.clear();
48 attr.addAttribute("", "", "id", "", "0" + i);
49 handler.startElement("", "", "book", attr);
51 // 创建name
52 attr.clear();
53 handler.startElement("", "", "name", attr);
54 String name = "Java程序设计" + i;
55 handler.characters(name.toCharArray(), 0, name.length());
56 handler.endElement("", "", "name");
58 // 创建author
59 attr.clear();
60 handler.startElement("", "", "author", attr);
61 String author = "张三" + i;
62 handler.characters(author.toCharArray(), 0, author.length());
63 handler.endElement("", "", "author");
65 // 创建price
66 attr.clear();
67 attr.addAttribute("", "", "type", "", "元");
68 handler.startElement("", "", "price", attr);
69 double price = 60 + i;
70 handler.characters(("" + price).toCharArray(), 0,
71 ("" + price).length());
72 handler.endElement("", "", "price");
74 handler.endElement("", "", "book");
75 }
76 handler.endElement("", "", "bookstore");
77 // 关闭document
78 handler.endDocument();
79 System.out.println("生成book2.xml成功");
80 } catch (Exception e) {
81 e.printStackTrace();
82 System.out.println("生成book2.xml失败");
83 }
84 }
86 public static void main(String[] args) {
87 Long start = System.currentTimeMillis();
88 createXml();
89 System.out.println("运行时间:" + (System.currentTimeMillis() - start));
90 }
运行结果:
3.DOM4j
1 import java.io.File;
2 import java.io.FileOutputStream;
4 import org.dom4j.Document;
5 import org.dom4j.DocumentHelper;
6 import org.dom4j.Element;
7 import org.dom4j.io.OutputFormat;
8 import org.dom4j.io.XMLWriter;
10 public class Dom4jToXmlDemo {
12 /**
13 * 生成xml方法
14 */
15 public static void createXml() {
16 try {
17 // 1、创建document对象
18 Document document = DocumentHelper.createDocument();
19 // 2、创建根节点bookstore
20 Element bookstore = document.addElement("bookstore");
21 // 3、向bookstore节点添加version属性
22 bookstore.addAttribute("address", "新华书店");
23 // 4、生成子节点及子节点内容
24 Element book = bookstore.addElement("book");
25 book.addAttribute("id", "01");
26 Element name = book.addElement("name");
27 name.setText("Java程序设计");
28 Element author = book.addElement("author");
29 author.setText("张三");
30 Element price = book.addElement("price");
31 price.setText("60");
32 price.addAttribute("type", "元");
34 // 5、设置生成xml的格式
35 OutputFormat format = OutputFormat.createPrettyPrint();
36 // 设置编码格式
37 format.setEncoding("UTF-8");
39 // 6、生成xml文件
40 File file = new File("book3.xml");
41 XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
42 // 设置是否转义,默认使用转义字符
43 writer.setEscapeText(false);
44 writer.write(document);
45 writer.close();
46 System.out.println("生成book3.xml成功");
47 } catch (Exception e) {
48 e.printStackTrace();
49 System.out.println("生成book3.xml失败");
50 }
51 }
53 public static void main(String[] args) {
54 Long start = System.currentTimeMillis();
55 createXml();
56 System.out.println("运行时间:" + (System.currentTimeMillis() - start));
57 }
运行结果:
4.JDOM
1 import java.io.File;
2 import java.io.FileOutputStream;
4 import org.jdom2.Document;
5 import org.jdom2.Element;
6 import org.jdom2.output.Format;
7 import org.jdom2.output.XMLOutputter;
9 public class JdomToXmlDemo {
11 /**
12 * 生成xml方法
13 */
14 public static void createXml() {
15 try {
16 // 1、生成一个根节点
17 Element bookstore = new Element("bookstore");
18 // 2、为节点添加属性
19 bookstore.setAttribute("address", "新华书店");
20 // 3、生成一个document对象
21 Document document = new Document(bookstore);
23 Element book = new Element("book");
24 book.setAttribute("id", "01");
25 bookstore.addContent(book);
26 Element name = new Element("name");
27 name.setText("Java程序设计");
28 book.addContent(name);
29 Element author = new Element("author");
30 author.setText("张三");
31 book.addContent(author);
32 Element price = new Element("price");
33 price.setText("60");
34 price.setAttribute("type", "元");
35 book.addContent(price);
37 Format format = Format.getCompactFormat();
38 // 设置换行Tab或空格
39 format.setIndent(" ");
40 format.setEncoding("UTF-8");
42 // 4、创建XMLOutputter的对象
43 XMLOutputter outputer = new XMLOutputter(format);
45 // 5、利用outputer将document转换成xml文档
46 File file = new File("book4.xml");
47 outputer.output(document, new FileOutputStream(file));
49 System.out.println("生成book4.xml成功");
50 } catch (Exception e) {
51 System.out.println("生成book4.xml失败");
52 }
53 }
55 public static void main(String[] args) {
56 Long start = System.currentTimeMillis();
57 createXml();
58 System.out.println("运行时间:" + (System.currentTimeMillis() - start));
59 }
运行结果: