l导出数据的应用框架,通常对于具体的导出内容和格式是有要求的:
l分成三部分,文件头、文件体、文件尾
l文件头:分公司编号、导出数据的日期,对于文本格式,中间用逗号分离
l文件体:表名称,然后分条描述数据。。。。
l文件尾:输出人
l不管是输出文本文件,还是输出XML文件,步骤基本一致
l先拼接文件头的内容
l然后拼接文件体的内容
l在拼接文件尾的内容
l最后把拼接好的内容输出去成为文件
public abstract class ExportBuilder {
protected String id;
protected String date;
protected String TableName;
protected String data;
protected String Person;
public ExportBuilder(String id, String date, String tableName, String data,
String person) {
this.id = id;
this.date = date;
TableName = tableName;
this.data = data;
Person = person;
}
public abstract void BuildHead();
public abstract void BuildBody();
public abstract void BuildFoot();
}
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class ExportXML extends ExportBuilder{
private Document document;
private Element container;
public ExportXML(String id, String date, String tableName, String data,
String person) throws ParserConfigurationException
{
super(id, date, tableName, data, person);
// 创建解析器工厂
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
document = db.newDocument();
}
@Override
public void BuildHead() {
document.setXmlStandalone(true);
container=document.createElement("Conatiner");
Element head=document.createElement("Head");
Element Id = document.createElement("Id");
Id.setTextContent(id);
Element Date = document.createElement("Date");
Date.setTextContent(date);
head.appendChild(Id);
head.appendChild(Date);
container.appendChild(head);
}
@Override
public void BuildBody() {
Element Body=document.createElement("body");
Element tname = document.createElement("TableName");
tname.setTextContent(TableName);
Element Data = document.createElement("Data");
Data.setTextContent(data);
Body.appendChild(Data);
Body.appendChild(tname);
container.appendChild(Body);
}
@Override
public void BuildFoot() {
Element foot=document.createElement("foot");
Element per = document.createElement("Person");
per.setTextContent(Person);
foot.appendChild(per);
container.appendChild(foot);
document.appendChild(container);
}
public void Result() throws TransformerException
{TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
tf.setOutputProperty(OutputKeys.INDENT, "yes");
// 创建xml文件并写入内容
tf.transform(new DOMSource(document), new StreamResult(new File("g:/shujv.xml")));
}
}
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class TxtBuilder extends ExportBuilder
{public TxtBuilder(String id, String date, String tableName, String data,
String person) {
super(id, date, tableName, data, person);
// TODO 自动生成的构造函数存根
}
private String str="";
@Override
public void BuildHead() {
str+=id+","+date+"\r\n";
}
@Override
public void BuildBody() {
str+=TableName+"\r\n";
str+=data+"\r\n";
}
@Override
public void BuildFoot() {
str+=Person+"\r\n";
}
public void Result() throws IOException
{File f1=new File("d:\\gwh.txt");
FileWriter fw=new FileWriter(f1);
BufferedWriter bw=new BufferedWriter(fw);
bw.write(str);
bw.close();
}
}
public class Director {
private ExportBuilder eb;
public Director(ExportBuilder eb) {
this.eb = eb;
}
public void Build()
{eb.BuildHead();
eb.BuildBody();
eb.BuildFoot();
}
}
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
public class Test {
public static void main(String args[]) throws IOException, ParserConfigurationException, TransformerException
{TxtBuilder tb=new TxtBuilder("001","2019/1/2","产品数目","20","王刚");
Director dr=new Director(tb);
dr.Build();
tb.Result();
ExportXML ex=new ExportXML("002","2019/1/5","价格","30","李辉");
dr=new Director(ex);
dr.Build();
ex.Result();
}
}