import java.sql.*;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import java.io.FileWriter;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.io.FileOutputStream;
public class GetUserListXML {
Connection conn;
public GetUserListXML(String file) {
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdb:oracle:thin:@ruang:1521:webapp","xling","webapp");
Statement stmt = conn.createStatement();
ResultSet rst = stmt.executeQuery("SELECT USER_NO,USER_NAME,SEX,COMP_NO,REGISTER_DATE FROM G_USER");
this.exportToXML(rst, file);
conn.close();
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e1){
e1.printStackTrace();
}
}
private void exportToXML(ResultSet rst,String file){
Document doc = DocumentHelper.createDocument();
//doc.setXMLEncoding("UTF-8");
Element root = doc.addElement("UserList");
root.addAttribute("createTime", (new Date()).toString());
Element user;
try{
while(rst.next()){
System.out.println(rst.getString(2));
user = root.addElement("User");
user.addAttribute("userNo", rst.getString(1));
user.addAttribute("userName", rst.getString(2));
user.addAttribute("sex", rst.getString(3));
user.addAttribute("compNo", rst.getString(4));
user.addAttribute("registerDate", rst.getString(5));
}
root.addAttribute("count",(new Integer(rst.getRow() + 1)).toString());
}catch(SQLException e){
//
}
OutputFormat opf = OutputFormat.createCompactFormat();
opf.setEncoding("UTF-8");//当用FileWriter时,UTF-8编码输出的文件用Notepad打开,中文是乱码?
//FileWriter fw;
FileOutputStream fos;
XMLWriter xw;
try{
//fw = new FileWriter(new File(file));
fos = new FileOutputStream(new File(file));
//xw = new XMLWriter(fw,opf);
xw = new XMLWriter(fos);
xw.write(doc);
xw.close();
//fw.close();
fos.close();
}catch(IOException e){
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
GetUserListXML userList = new GetUserListXML("userList.XML");
}
}
////////////////////////////////////读取xml
import java.io.File;
import java.util.Iterator;
import org.dom4j.io.SAXReader;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Attribute;
public class First {
public Document parse(String sFile) throws DocumentException {
SAXReader reader = new SAXReader();
Document doc = reader.read(new File(sFile));
return doc;
}
public void show(Document doc){
System.out.println(doc.getName());
Element root = doc.getRootElement();
System.out.println(doc.getName());
System.out.println(doc.getName());
subShow(root,1);
}
public void subShow(Element elem,int leval){
Iterator it = elem.elementIterator();
Element subElem;
while(it.hasNext()){
subElem = (Element) it.next();
System.out.print(this.getPrefix(leval) + subElem.getName());
showAttribute(subElem);
if(subElem.nodeCount() == 1)
System.out.println(subElem.getStringValue());
else
System.out.println();
subShow(subElem,leval + 1);
}
}
public void showAttribute(Element elem){
Iterator it = elem.attributeIterator();
Attribute att;
System.out.print("[");
while(it.hasNext()){
att = (Attribute) it.next();
System.out.print(" " + att.getName() + "=" + att.getValue());
}
System.out.print("]");
}
private String getPrefix(int len){
return String.format("%" + len * 2 + "s", " ");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
First first = new First();
try{
first.show(first.parse("D:\\WEB\\WEBAPP\\WEBAPP_STRUTS_MODULE\\WEB-INF\\web.xml"));
}catch(DocumentException e){
System.out.println("Failure");
}
}
}
阅读(939) | 评论(0) | 转发(0) |