Posted on 2009-06-19 09:10
周竞先 阅读(1572)
评论(0) 编辑 收藏 所属分类:
J2EE 、
xml
很久没用xml了,最近项目应该会用到,故昨晚回家写了个测试程序,代码如下:
1 package com.potevio.telecom;
2
3 //文件类
4 import java.io.File;
5 import java.io.FileNotFoundException;
6
7 //文件输出类
8 import java.io.FileOutputStream;
9 import java.io.IOException;
10
11 //负责解析的类
12 import javax.xml.parsers.DocumentBuilder;
13 import javax.xml.parsers.DocumentBuilderFactory;
14 import javax.xml.parsers.ParserConfigurationException;
15
16 //映射类
17 import javax.xml.transform.Transformer;
18 import javax.xml.transform.TransformerConfigurationException;
19 import javax.xml.transform.TransformerException;
20 import javax.xml.transform.TransformerFactory;
21
22 //xml映射输入和输出类
23 import javax.xml.transform.dom.DOMSource;
24 import javax.xml.transform.stream.StreamResult;
25
26 //节点类
27 import org.w3c.dom.Comment;
28 import org.w3c.dom.Document;
29 import org.w3c.dom.Element;
30 import org.w3c.dom.Node;
31 import org.w3c.dom.NodeList;
32
33 /**
34 * @description 构造一个北京到长沙的简单列车时刻表.用DOM的方式实现.
35 *
36 * @author Zhou-Jingxian
37 *
38 * @date Jun 18, 2009
39 *
40 */
41 public class CreateXMLContent {
42
43 public static void main(String args[]){
44
45 try {
46 //构造的原始数据对象
47 String train[] = {"T1次","K185次","Z17次"};
48 String type[] = {"特快","普快","直达"};
49 String startTime[] = {"15:45","11:47","18:10"};
50
51 //解析器工厂类
52 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
53
54 //解析器
55 DocumentBuilder builder = factory.newDocumentBuilder();
56
57 //操作的Document对象
58 Document document = builder.newDocument();
59
60 //设置XML的版本
61 document.setXmlVersion("1.0");
62
63 //添加注释
64 Comment xmlComment = document.createComment("本文档中内容主要用于测试!");
65
66 //将注释添加到xml中
67 document.appendChild(xmlComment);
68
69 //设置根节点名称
70 Element traintimelist = document.createElement("火车时刻表");
71
72 //把节点添加到根节点
73 document.appendChild(traintimelist);
74
75 for(int k = 1; k <=train.length; k++){
76 traintimelist.appendChild(document.createElement("车次"));
77 }
78
79 NodeList nodeList = document.getElementsByTagName("车次");
80 int size = nodeList.getLength();
81 for(int k = 0; k<size; k++){
82 Node node = nodeList.item(k);
83 if(node.getNodeType() == Node.ELEMENT_NODE){
84 Element elementNode = (Element)node;
85 elementNode.setAttribute("类别", type[k]);
86 elementNode.appendChild(document.createElement("名字"));
87 elementNode.appendChild(document.createElement("开车时间"));
88
89 }
90 }
91
92 nodeList = document.getElementsByTagName("名字");
93 size = nodeList.getLength();
94 for(int k = 0; k<size; k++){
95 Node node = nodeList.item(k);
96 if(node.getNodeType() == Node.ELEMENT_NODE){
97 Element elementNode = (Element)node;
98 elementNode.appendChild(document.createTextNode(train[k]));
99
100 }
101 }
102
103 nodeList = document.getElementsByTagName("开车时间");
104 size = nodeList.getLength();
105 for(int k = 0; k<size; k++){
106 Node node = nodeList.item(k);
107 if(node.getNodeType() == Node.ELEMENT_NODE){
108 Element elementNode = (Element)node;
109 elementNode.appendChild(document.createTextNode(startTime[k]));
110
111 }
112 }
113
114 //开始把Document映射到文件
115 TransformerFactory transFactory = TransformerFactory.newInstance();
116 Transformer transformer = transFactory.newTransformer();
117
118 //设置输出结果
119 DOMSource domSource = new DOMSource(document);
120
121 //生成xml文件
122 File file = new File("北京到长沙火车时刻表.xml");
123
124 //判断是否存在,如果不存在,则创建
125 if(!file.exists()){
126 file.createNewFile();
127 }
128
129 //文件输出流
130 FileOutputStream out = new FileOutputStream(file);
131
132 //设置输入源
133 StreamResult xmlResult = new StreamResult(out);
134
135 //输出xml文件
136 transformer.transform(domSource, xmlResult);
137
138 //测试文件输出的路径
139 System.out.println(file.getAbsolutePath());
140
141 } catch (ParserConfigurationException e) {
142
143 e.printStackTrace();
144 } catch (TransformerConfigurationException e) {
145
146 e.printStackTrace();
147 } catch (FileNotFoundException e) {
148
149 e.printStackTrace();
150 } catch (TransformerException e) {
151
152 e.printStackTrace();
153 } catch (IOException e) {
154
155 e.printStackTrace();
156
157 }finally{
158
159 }
160
161 }
162 }
163
运行的打印地址:
D:\MyEclipse 6.0\workspace\WriteAndReadXML\北京到长沙火车时刻表.xml
将上面的地址放到(IE)地址栏显示的效果图下:
Life,simple and happy!