java代码导出xml java代码导出设置sheet的名字
怎样通过java定时将数据库中的信息导出后生成xml文件,并通过ftp上传到指定的位置
1、使用quarz或者jdk的timer来制定定时任务。
10年积累的成都网站设计、成都网站制作、外贸网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先做网站后付款的网站建设流程,更有莒南免费网站建设让你可以放心的选择与我们合作。
2、使用jdbc或者hibernate等方法获取数据库中信息。
3、使用xmlbeans或者dom4j等技术生成xml文件。
4、使用sun.net.上传到指定ftp服务器。
怎么使用java导出大数据为xml文件
dom4j可以试试,不知道你的数据量有多大,如果太大的话,我没试过
xml文件是有规律的,你可以把要导出的数据构造一下,
我有个简单的代码
package com.test.xml;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class Dom4JXML {
public void createXML() {
//用工厂类创建一个document实例
Document doc = DocumentHelper.createDocument();
//创建根元素emps
Element rootEle = doc.addElement("emps");
//添加注释
rootEle.addComment("这是一个dom4j生成的xml文件");
//emps根节点下创建一个emp节点
Element empEle = rootEle.addElement("emp");
//emp添加属性id="1"
empEle.addAttribute("id", "1");
//emp节点下创建一个name节点
Element nameEle = empEle.addElement("name");
//name节点下创建一个文本节点zhangsan
nameEle.setText("zhangsan");
//再为name节点创建一个兄弟节点
Element sexEle = empEle.addElement("sex");
sexEle.setText("man");
//将document中的内容写入文件中
try {
Writer out = new FileWriter("F:\\emps.xml");
//格式化输出,类型IE浏览一样
OutputFormat format = OutputFormat.createPrettyPrint();
//OutputFormat format = OutputFormat.createCompactFormat();
format.setEncoding("UTF-8");
//创建写出对象
XMLWriter writer = new XMLWriter(out,format);
writer.write(doc);
writer.close();
System.out.println("生成emps.xml成功。");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("失败了。");
}
}
public static void main(String[] args) {
new Dom4JXML().createXML();
}
}
然而从xml文件中解析以后的数据收集可以用SAX试试看
我这里有个简单的例子
package com.test.xml;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SAXParseHandler extends DefaultHandler{
public void startDocument()throws SAXException{
System.out.println("起始文挡");
}
public void endDocument()throws SAXException{
System.out.println("结束文挡");
}
public void characters(char[] ch,int start,int length)throws SAXException{
String charString=new String(ch,start,length);
System.out.println("字符:"+charString);
}
public void startElement(String namespaceURI,String localName,String qName,Attributes atts)throws SAXException{
System.out.println("起始元素:"+qName);
for(int i=0;iatts.getLength();i++){
System.out.println("属性值:"+atts.getValue(i));
}
}
public void endElement(String namespaceURI,String localName,String qName)throws SAXException{
System.out.println("结束元素:"+qName);
}
}
package com.test.xml;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
public class Books {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO 自动生成方法存根
SAXParserFactory factory=SAXParserFactory.newInstance();
SAXParser parser=factory.newSAXParser();
parser.parse("booksamp.xml",new SAXParseHandler());
}
}
JAVA获取XML文件并输出
// 给你一个例子,你参考下
// java通过dom读写xml文件
/*要读的xml文件
?xml version="1.0" encoding="GB2312"?
学生花名册
学生 性别 = "男"
姓名李华/姓名
年龄14/年龄
/学生
学生 性别 = "男"
姓名张三/姓名
年龄16/年龄
/学生
/学生花名册
*/
package xml;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Iterator;
import java.util.Vector;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
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;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
public class DomTest {
Vector students_Vector;
private Vector readXMLFile(String file) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder.parse(file); // 获取到xml文件
// 下面开始读取
Element root = doc.getDocumentElement(); // 获取根元素
NodeList students = root.getElementsByTagName_r("学生");
students_Vector = new Vector();
for (int i = 0; i students.getLength(); i++) {
// 一次取得每一个学生元素
Element ss = (Element) students.item(i);
// 创建一个学生的实例
student stu = new student();
stu.setSex(ss.getAttribute("性别"));
NodeList names = ss.getElementsByTagName_r("姓名");
Element e = (Element) names.item(0);
Node t = e.getFirstChild();
stu.setName(t.getNodeValue());
NodeList ages = ss.getElementsByTagName_r("年龄");
e = (Element) ages.item(0);
t = e.getFirstChild();
stu.setAge(Integer.parseInt(t.getNodeValue()));
students_Vector.add(stu);
}
return students_Vector;
}
// 写入xml文件
public static void callWriteXmlFile(Document doc, Writer w, String encoding) {
try {
Source source = new DOMSource(doc);
Result result = new StreamResult(w);
Transformer xformer = TransformerFactory.newInstance()
.newTransformer();
xformer.setOutputProperty(OutputKeys.ENCODING, encoding);
xformer.transform(source, result);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
private void writeXMLFile(String outfile) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = dbf.newDocumentBuilder();
} catch (Exception e) {
}
Document doc = builder.newDocument();
Element root = doc.createElement("学生花名册");
doc.appendChild(root); // 将根元素添加到文档上
// 获取学生信息
for (int i = 0; i students_Vector.size(); i++) {
student s = (student) students_Vector.get(i);
// 创建一个学生
Element stu = doc.createElement("学生");
stu.setAttribute("性别", s.getSex());
root.appendChild(stu);// 添加属性
// 创建文本姓名节点
Element name = doc.createElement("姓名");
stu.appendChild(name);
Text tname = doc.createTextNode(s.getName());
name.appendChild(tname);
//创建文本年龄节点
Element age = doc.createElement("年龄");
stu.appendChild(age); // 将age添加到学生节点上
Text tage = doc.createTextNode(String.valueOf(s.getAge()));
age.appendChild(tage); // 将文本节点放在age节点上
}
try {
FileOutputStream fos = new FileOutputStream(outfile);
OutputStreamWriter outwriter = new OutputStreamWriter(fos);
// ((XmlDocument)doc).write(outwriter); //出错!
callWriteXmlFile(doc, outwriter, "gb2312");
outwriter.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
String str = "xml/student.xml";
DomTest t = new DomTest();
try {
Vector v = t.readXMLFile(str);
Iterator it = v.iterator();
while (it.hasNext()) {
student s = (student) it.next();
System.out.println(s.getName() + "\t" + s.getAge() + "\t"
+ s.getSex());
}
} catch (Exception e) {
e.printStackTrace();
}
String outfile = "xml/stucopy.xml";
t.writeXMLFile(outfile);
}
}
class student{
private String sex;
private String name;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void setSex(String s){sex=s;}
public String getSex(){return sex;}
public void setName(String n){name=n;}
public String getName(){return name;}
}
JAVA读取本地TXT文件转换为XML文件输出
txt文件格式如下,采用的是一个服务端tomcat的log日志
[06/Mar/2013:18:43:55] 110.110.1.1 GET /rest/keyword/getallids/aid/3/os/android2.3.5/ver/1.0/ HTTP/1.1 200 210 1453 1.453 'Dalvik/1.4.0 (Linux; U; Android 2.3.5; T550 Build/MocorDroid2.3.5)'
[06/Mar/2013:18:43:55] 110.110.1.1 GET /rest/keyword/getallids/aid/3/os/android2.3.4/ver/1.0/ HTTP/1.1 200 210 1453 1.453 'Dalvik/1.4.0 (Linux; U; Android 2.3.4; LG-E730 Build/GRJ22)'
[06/Mar/2013:18:43:55] 110.110.1.1 GET /rest/keyword/getallids/aid/3/os/android2.3.5/ver/1.0/ HTTP/1.1 200 210 1453 1.453 'Dalvik/1.4.0 (Linux; U; Android 2.3.5; MI-ONE Plus Build/GINGERBREAD)'
[06/Mar/2013:18:43:55] 110.110.1.1 GET /rest/keyword/getallids/aid/3/os/android2.3.6/ver/1.0/ HTTP/1.1 200 210 1453 1.453 'Dalvik/1.4.0 (Linux; U; Android 2.3.6; GT-I9220 Build/GINGERBREAD)'
[06/Mar/2013:18:43:55] 110.110.1.1 POST /rest/keyword/getallids/aid/3/os/android4.0.3/ver/1.0/ HTTP/1.1 200 210 1469 1.469 'Dalvik/1.6.0 (Linux; U; Android 4.0.3; HUAWEI C8812 Build/HuaweiC8812)'
[06/Mar/2013:18:43:55] 110.110.1.1 GET /rest/keyword/getallids/aid/3/os/android2.2.2/ver/1.0/ HTTP/1.1 200 210 1453 1.453 'Dalvik/1.2.0 (Linux; U; Android 2.2.2; HUAWEI T8300 Build/FRF91)'
[06/Mar/2013:18:43:55] 110.110.1.1 GET /rest/keyword/getallids/aid/3/os/android2.3.6/ver/1.0/ HTTP/1.0 200 210 1484 1.484 'Dalvik/1.4.0 (Linux; U; Android 2.3.6; Mytel Build/GRK39F)'
[06/Mar/2013:18:43:55] 110.110.1.1 POST /rest/keyword/getallids/aid/3/os/android4.1.1/ver/1.0/ HTTP/1.1 200 210 1406 1.406 'Dalvik/1.6.0 (Linux; U; Android 4.1.1; GT-N7100 Build/JRO03C)'
转换代码如下
package test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
public class promble04 {
@SuppressWarnings("resource")
public static void txtToXML(String txtPath) throws IOException
{
File file = new File(txtPath);
Document document = DocumentHelper.createDocument();
Element list = document.addElement("list");
BufferedReader br = new BufferedReader(new FileReader(file));
String line = br.readLine();
while(line!=null)
{
String temp[] = line.split(" ");
Element log = list.addElement("log");
Element time = log.addElement("time");
Element ip = log.addElement("ip");
Element httpType = log.addElement("httpType");
Element url = log.addElement("url");
Element httplevel = log.addElement("httplevel");
Element linuxVersion = log.addElement("linuxVersion");
Element os = log.addElement("os");
Element AndroidVersion = log.addElement("AndroidVersion");
Element phoneType = log.addElement("phoneType");
for(int i = 0;itemp.length;i++)
{
time.setText(temp[0]);
ip.setText(temp[1]);
httpType.setText(temp[2]);
url.setText(temp[3]);
httplevel.setText(temp[4]);
linuxVersion.setText(temp[9]);
os.setText(temp[10]);
AndroidVersion.setText(temp[12]);
phoneType.setText(temp[13]);
}
line = br.readLine();
}
Writer filewriter = new FileWriter("D:\\log\\test.xml");
XMLWriter xmlWriter = new XMLWriter(filewriter);
xmlWriter.write(document);
xmlWriter.close();
}
public static void main(String args[]) throws IOException
{
promble04.txtToXML("D:\\log\\test.txt");
}
}
转换结果如下
?xml version="1.0" encoding="UTF-8"?
list
log
time[06/Mar/2013:18:43:55]/time
ip110.110.1.1/ip
httpTypeGET/httpType
url/rest/keyword/getallids/aid/3/os/android2.3.5/ver/1.0//url
httplevelHTTP/1.1/httplevel
linuxVersion'Dalvik/1.4.0/linuxVersion
os(Linux;/os
AndroidVersionAndroid/AndroidVersion
phoneType2.3.5;/phoneType
/log
log
time[06/Mar/2013:18:43:55]/time
ip110.110.1.1/ip
httpTypeGET/httpType
url/rest/keyword/getallids/aid/3/os/android2.3.4/ver/1.0//url
httplevelHTTP/1.1/httplevel
linuxVersion'Dalvik/1.4.0/linuxVersion
os(Linux;/os
AndroidVersionAndroid/AndroidVersion
phoneType2.3.4;/phoneType
/log
log
time[06/Mar/2013:18:43:55]/time
ip110.110.1.1/ip
httpTypeGET/httpType
url/rest/keyword/getallids/aid/3/os/android2.3.5/ver/1.0//url
httplevelHTTP/1.1/httplevel
linuxVersion'Dalvik/1.4.0/linuxVersion
os(Linux;/os
AndroidVersionAndroid/AndroidVersion
phoneType2.3.5;/phoneType
/log
log
time[06/Mar/2013:18:43:55]/time
ip110.110.1.1/ip
httpTypeGET/httpType
url/rest/keyword/getallids/aid/3/os/android2.3.6/ver/1.0//url
httplevelHTTP/1.1/httplevel
linuxVersion'Dalvik/1.4.0/linuxVersion
os(Linux;/os
AndroidVersionAndroid/AndroidVersion
phoneType2.3.6;/phoneType
/log
log
time[06/Mar/2013:18:43:55]/time
ip110.110.1.1/ip
httpTypePOST/httpType
url/rest/keyword/getallids/aid/3/os/android4.0.3/ver/1.0//url
httplevelHTTP/1.1/httplevel
linuxVersion'Dalvik/1.6.0/linuxVersion
os(Linux;/os
AndroidVersionAndroid/AndroidVersion
phoneType4.0.3;/phoneType
/log
log
time[06/Mar/2013:18:43:55]/time
ip110.110.1.1/ip
httpTypeGET/httpType
url/rest/keyword/getallids/aid/3/os/android2.2.2/ver/1.0//url
httplevelHTTP/1.1/httplevel
linuxVersion'Dalvik/1.2.0/linuxVersion
os(Linux;/os
AndroidVersionAndroid/AndroidVersion
phoneType2.2.2;/phoneType
/log
log
time[06/Mar/2013:18:43:55]/time
ip110.110.1.1/ip
httpTypeGET/httpType
url/rest/keyword/getallids/aid/3/os/android2.3.6/ver/1.0//url
httplevelHTTP/1.0/httplevel
linuxVersion'Dalvik/1.4.0/linuxVersion
os(Linux;/os
AndroidVersionAndroid/AndroidVersion
phoneType2.3.6;/phoneType
/log
log
time[06/Mar/2013:18:43:55]/time
ip110.110.1.1/ip
httpTypePOST/httpType
url/rest/keyword/getallids/aid/3/os/android4.1.1/ver/1.0//url
httplevelHTTP/1.1/httplevel
linuxVersion'Dalvik/1.6.0/linuxVersion
os(Linux;/os
AndroidVersionAndroid/AndroidVersion
phoneType4.1.1;/phoneType
/log
/list
分享标题:java代码导出xml java代码导出设置sheet的名字
浏览路径:http://myzitong.com/article/dojehis.html