public class DataOutputTimeXml {
//文件保存到xml文件
public void saveExportDateToXml(String loginname)
{
try {
XMLWriter writer = null;// 声明写XML的对象
SAXReader reader = new SAXReader();
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");// 设置XML文件的编码格式
String path= System.getProperty("user.dir");//???E:\neweclipsewprkspace\toponymcensus
String filePath = path+"//app"+"//datefile"+"//"+"date.xml";
//String filePath ="E://lxh.xml";
File file = new File(filePath);
if (file.exists()) {
Document document = reader.read(file);// 读取XML文件
Element root = document.getRootElement();// 得到根节点
boolean bl = true;
for (Iterator i = root.elementIterator("info"); i.hasNext();) {
Element info = (Element) i.next();
if (info.element("name").getTextTrim()!=null && loginname.equals(info.element("name").getTextTrim())) {
// 修改导出时间
info.element("date").setText(DateUtil.dateToString(new Date(), "yy-MM-dd HH:mm:ss"));
writer = new XMLWriter(new FileWriter(filePath), format);
writer.write(document);
writer.close();
bl = false;
break;
}
}
if (bl) {
// 添加一个信息
Element info = root.addElement("info");
Element name = info.addElement("name");
name.setText(loginname);
Element date = info.addElement("date");
date.setText(DateUtil.dateToString(new Date(), "yy-MM-dd HH:mm:ss"));
writer = new XMLWriter(new FileWriter(filePath), format);
writer.write(document);
writer.close();
}
} else {
// 新建.xml文件并新增内容
Document _document = DocumentHelper.createDocument();
Element _root = _document.addElement("Doucument");
Element _info = _root.addElement("info");
Element _username = _info.addElement("name");
_username.setText(loginname);
Element _date = _info.addElement("date");
_date.setText(DateUtil.dateToString(new Date(), "yy-MM-dd HH:mm:ss"));
writer = new XMLWriter(new FileWriter(file), format);
writer.write(_document);
writer.close();
}
System.out.println("操作结束! ");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 根据用户名得到上次时间
* @param loginname
* @return
*/
//解析.xml文件
public String getDateByLoginName(String loginname)
{
String returnDate ="";
try{
SAXReader reader = new SAXReader();
String path= System.getProperty("user.dir");//???E:\neweclipsewprkspace\toponymcensus
String filePath = path+"//app"+"//datefile"+"//"+"date.xml";
//String filePath ="E://lxh.xml";
File file = new File(filePath);
if (file.exists()) {
Document document = reader.read(file);// 读取XML文件
Element root = document.getRootElement();// 得到根节点
boolean bl = true;
for (Iterator i = root.elementIterator("info"); i.hasNext();) {
Element info = (Element) i.next();
if (info.element("name").getTextTrim()!=null && loginname.equals(info.element("name").getTextTrim())) {
returnDate = info.element("date").getTextTrim();
}
}
}
return returnDate;
}catch(Exception e)
{
e.printStackTrace();
return null;
}
}
}
//文件保存到xml文件
public void saveExportDateToXml(String loginname)
{
try {
XMLWriter writer = null;// 声明写XML的对象
SAXReader reader = new SAXReader();
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");// 设置XML文件的编码格式
String path= System.getProperty("user.dir");//???E:\neweclipsewprkspace\toponymcensus
String filePath = path+"//app"+"//datefile"+"//"+"date.xml";
//String filePath ="E://lxh.xml";
File file = new File(filePath);
if (file.exists()) {
Document document = reader.read(file);// 读取XML文件
Element root = document.getRootElement();// 得到根节点
boolean bl = true;
for (Iterator i = root.elementIterator("info"); i.hasNext();) {
Element info = (Element) i.next();
if (info.element("name").getTextTrim()!=null && loginname.equals(info.element("name").getTextTrim())) {
// 修改导出时间
info.element("date").setText(DateUtil.dateToString(new Date(), "yy-MM-dd HH:mm:ss"));
writer = new XMLWriter(new FileWriter(filePath), format);
writer.write(document);
writer.close();
bl = false;
break;
}
}
if (bl) {
// 添加一个信息
Element info = root.addElement("info");
Element name = info.addElement("name");
name.setText(loginname);
Element date = info.addElement("date");
date.setText(DateUtil.dateToString(new Date(), "yy-MM-dd HH:mm:ss"));
writer = new XMLWriter(new FileWriter(filePath), format);
writer.write(document);
writer.close();
}
} else {
// 新建.xml文件并新增内容
Document _document = DocumentHelper.createDocument();
Element _root = _document.addElement("Doucument");
Element _info = _root.addElement("info");
Element _username = _info.addElement("name");
_username.setText(loginname);
Element _date = _info.addElement("date");
_date.setText(DateUtil.dateToString(new Date(), "yy-MM-dd HH:mm:ss"));
writer = new XMLWriter(new FileWriter(file), format);
writer.write(_document);
writer.close();
}
System.out.println("操作结束! ");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 根据用户名得到上次时间
* @param loginname
* @return
*/
//解析.xml文件
public String getDateByLoginName(String loginname)
{
String returnDate ="";
try{
SAXReader reader = new SAXReader();
String path= System.getProperty("user.dir");//???E:\neweclipsewprkspace\toponymcensus
String filePath = path+"//app"+"//datefile"+"//"+"date.xml";
//String filePath ="E://lxh.xml";
File file = new File(filePath);
if (file.exists()) {
Document document = reader.read(file);// 读取XML文件
Element root = document.getRootElement();// 得到根节点
boolean bl = true;
for (Iterator i = root.elementIterator("info"); i.hasNext();) {
Element info = (Element) i.next();
if (info.element("name").getTextTrim()!=null && loginname.equals(info.element("name").getTextTrim())) {
returnDate = info.element("date").getTextTrim();
}
}
}
return returnDate;
}catch(Exception e)
{
e.printStackTrace();
return null;
}
}
}