1.xml文件生成,拼接字符串使用StringBuffer或StringBuilder
2.拼接好后写入文件即可,将多个实体类写入xml文件
3.这种方式比较简单,但是操作也比较麻烦
4.下面是我的代码:
@1测试代码,数据是模拟的productXmlFile是测试类
package operator.hui.xml; import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; /*
* 生成xml文件的类
*/
public class CreateXmlFile { public static void main(String[] args) {
// TODO Auto-generated method stub
List<Sms> lists = new ArrayList<Sms>();
for (int i = 0; i < 10; i++) {
Sms s = new Sms();
s.setAddr("1870087098"+i);
s.setContent("小明,你女朋友来了");
s.setDate("2000"+"-"+i+"-"+i+2+" "+"12:16:37");
lists.add(s);
}
productXmlFile("f://sms.xml",lists);
test(); } private static void test() {
// TODO Auto-generated method stub
System.out.println(CreateXmlFile.class.getSimpleName());
} /*
* 生成名为name的根节点为root的xml文件,这个文件存储lists这个集合的数据,path为文件路径
*
*/
public static void productXmlFile(String path,List<Sms> lists){ StringBuffer bf = new StringBuffer();
//组拼xml文件头
bf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
bf.append("\n");
//组拼根节点
bf.append("<smss>");
bf.append("\n");
for (Iterator iterator = lists.iterator(); iterator.hasNext();) {
Sms sms = (Sms) iterator.next();
System.out.println(sms.getAddr());
sms.productPoint(bf); }
bf.append("\n");
bf.append("</smss>"); File f = new File(path);
try {
FileOutputStream fos = new FileOutputStream(f);
fos.write(bf.toString().getBytes("utf8"));
fos.flush();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } }
@2javabean类Sms,实现了生成节点的接口
package operator.hui.xml; import java.util.Date; /**
* 短信数据封装类
* @author Administrator
*
*/
public class Sms implements ProductRuler{ //短信来源
private String addr;
//短信内容
private String content;
//短息日期
private String date;
public Sms() {
// TODO Auto-generated constructor stub
} public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
} @Override
public void productPoint(StringBuffer sb) {
sb.append("\t");
sb.append("<"+this.getClass().getSimpleName()+">");
sb.append("\n");
sb.append("\t\t");
sb.append("<addr>");
sb.append(this.addr);
sb.append("</addr>");
sb.append("\n");
sb.append("\t\t");
sb.append("<content>");
sb.append(this.content);
sb.append("</content>");
sb.append("\n");
sb.append("\t\t");
sb.append("<date>");
sb.append(this.date);
sb.append("</date>");
sb.append("\n");
sb.append("\t");
sb.append("</"+this.getClass().getSimpleName()+">");
sb.append("\n"); } }
@3生成节点的接口,只有一个方法,
package operator.hui.xml; /*
* 具体的xml内部节点生成规则
*/
public interface ProductRuler { /*在sb中生成节点*/
void productPoint(StringBuffer sb); }
@4运行结果:
Console控制台
生成的xml文件
生成的 文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<smss>
<Sms>
<addr>18700870980</addr>
<content>小明,你女朋友来了</content>
<date>2000-0-010 12:16:37</date>
</Sms>
<Sms>
<addr>18700870981</addr>
<content>小明,你女朋友来了</content>
<date>2000-1-110 12:16:37</date>
</Sms>
<Sms>
<addr>18700870982</addr>
<content>小明,你女朋友来了</content>
<date>2000-2-210 12:16:37</date>
</Sms>
<Sms>
<addr>18700870983</addr>
<content>小明,你女朋友来了</content>
<date>2000-3-310 12:16:37</date>
</Sms>
<Sms>
<addr>18700870984</addr>
<content>小明,你女朋友来了</content>
<date>2000-4-410 12:16:37</date>
</Sms>
<Sms>
<addr>18700870985</addr>
<content>小明,你女朋友来了</content>
<date>2000-5-510 12:16:37</date>
</Sms>
<Sms>
<addr>18700870986</addr>
<content>小明,你女朋友来了</content>
<date>2000-6-610 12:16:37</date>
</Sms>
<Sms>
<addr>18700870987</addr>
<content>小明,你女朋友来了</content>
<date>2000-7-710 12:16:37</date>
</Sms>
<Sms>
<addr>18700870988</addr>
<content>小明,你女朋友来了</content>
<date>2000-8-810 12:16:37</date>
</Sms>
<Sms>
<addr>18700870989</addr>
<content>小明,你女朋友来了</content>
<date>2000-9-910 12:16:37</date>
</Sms> </smss>