一条一条读出来生成xml呀,我就作过, Connection conn = this.getPersistenceManager().getSession().connection(); Element e = new Element("DATAPACK"); Document docJDOM = new Document(e);
//Tables是我预先写好的一个类,他里面的数组tablenames存放了我要查的表和sql语句(我不是每个表都查询全部内容的) for (int i = 0; i < Tables.tablenames.length; i++) { String sql = Tables.tablenames[i][1]; PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); List lst = new ArrayList(); //取字段名 ResultSetMetaData metaDate = rs.getMetaData(); int number = metaDate.getColumnCount(); String[] column = new String[number]; for (int j = 0; j < column.length; j++) { column[j] = metaDate.getColumnName(j + 1); } while (rs.next()) { String[] temp = new String[number]; for (int k = 0; k < temp.length; k++) { temp[k] = String.valueOf(rs.getObject(k + 1)); } lst.add(temp); }
//关闭 rs.close(); ps.close(); Element name = new Element("TABLE"); //设置属性name为表名 name.setAttribute("name", Tables.tablenames[i][0]); e.addContent(name); Iterator iter = lst.iterator(); while (iter.hasNext()) { Element te = new Element("ROW"); String[] temp = (String[]) iter.next(); for (int n = 0; n < column.length; n++) { //数据库为null的我就舍弃了 if (!temp[n].equals("null")) { te.setAttribute(column[n], temp[n]); } else { te.setAttribute(column[n], ""); } } name.addContent(te); } } Format format = Format.getCompactFormat(); format.setEncoding("GBK"); XMLOutputter fmt = new XMLOutputter(format);
String filePath = new String("c:\\"); FileWriter fwXML = new FileWriter(filePath + "/tableinfo.xml"); fmt.output(docJDOM, fwXML); fwXML.close();
别人就可以用你的程序直接生成XML所以你可以查一下是否有人做过这个东西
我觉得可以先把导出为文本格式
Connection conn = this.getPersistenceManager().getSession().connection();
Element e = new Element("DATAPACK");
Document docJDOM = new Document(e);
//Tables是我预先写好的一个类,他里面的数组tablenames存放了我要查的表和sql语句(我不是每个表都查询全部内容的)
for (int i = 0; i < Tables.tablenames.length; i++) {
String sql = Tables.tablenames[i][1];
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
List lst = new ArrayList(); //取字段名
ResultSetMetaData metaDate = rs.getMetaData();
int number = metaDate.getColumnCount();
String[] column = new String[number];
for (int j = 0; j < column.length; j++) {
column[j] = metaDate.getColumnName(j + 1);
} while (rs.next()) {
String[] temp = new String[number];
for (int k = 0; k < temp.length; k++) {
temp[k] = String.valueOf(rs.getObject(k + 1));
}
lst.add(temp);
}
//关闭
rs.close();
ps.close();
Element name = new Element("TABLE");
//设置属性name为表名
name.setAttribute("name", Tables.tablenames[i][0]);
e.addContent(name); Iterator iter = lst.iterator();
while (iter.hasNext()) {
Element te = new Element("ROW");
String[] temp = (String[]) iter.next();
for (int n = 0; n < column.length; n++) {
//数据库为null的我就舍弃了
if (!temp[n].equals("null")) {
te.setAttribute(column[n], temp[n]);
} else {
te.setAttribute(column[n], "");
}
}
name.addContent(te);
}
} Format format = Format.getCompactFormat();
format.setEncoding("GBK");
XMLOutputter fmt = new XMLOutputter(format);
String filePath = new String("c:\\");
FileWriter fwXML = new FileWriter(filePath + "/tableinfo.xml");
fmt.output(docJDOM, fwXML);
fwXML.close();