Java生成XML文件

我们在数据库中的数据可以将其提取出来生成XML文件,方便传输。例如数据库中有Admin这张表:

Java生成XML文件

我们写一个java类表示admin数据:

 package xmlDom.vo;

 import java.io.Serializable;
import java.util.Date;
import java.util.List; public class Admin implements Serializable
{
private String aid;
private String password;
private Integer rid;
private Integer type;
private Date lastdate;
private Integer flag ; public Integer getRid()
{
return rid;
}
public void setRid(Integer rid)
{
this.rid = rid;
}
public String getAid()
{
return aid;
}
public void setAid(String aid)
{
this.aid = aid;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public Integer getType()
{
return type;
}
public void setType(Integer type)
{
this.type = type;
}
public Date getLastdate()
{
return lastdate;
}
public void setLastdate(Date lastdate)
{
this.lastdate = lastdate;
}
public Integer getFlag()
{
return flag;
}
public void setFlag(Integer flag)
{
this.flag = flag;
} }

然后通过JDBC 将表中的数据转换成一个链表:

 package xmlDom.dbc;

 import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DatabaseConnection
{
private static final String MYSQLDRIVER = "org.gjt.mm.mysql.Driver";
private static final String MYSQLURL = "jdbc:mysql://localhost:3306/hrdb";
private static final String USERNAME = "root";
private static final String PASSWORD = "admin"; private Connection conn; public DatabaseConnection()
{
try
{
Class.forName(MYSQLDRIVER);
this.conn = DriverManager.getConnection(MYSQLURL, USERNAME, PASSWORD);
} catch (Exception e)
{
e.printStackTrace();
}
} public Connection getConnection() throws Exception
{
if (null != this.conn)
return this.conn;
else
throw new Exception("获取数据库连接失败");
} public void close()
{
if (null != this.conn)
{
try
{
this.conn.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
    public static List<Admin> loadFromDB() throws Exception
{
List<Admin> allAdmins = new ArrayList<Admin>(); DatabaseConnection dbc = new DatabaseConnection();
Connection conn = dbc.getConnection();
PreparedStatement ps = conn.prepareStatement("SELECT aid,rid,type,lastdate,flag FROM admin");
ResultSet rs = ps.executeQuery();
while(rs.next())
{
Admin vo = new Admin(); vo.setAid(rs.getString("aid"));
vo.setRid(rs.getInt("rid"));
vo.setType(rs.getInt("type"));
vo.setLastdate(rs.getDate("lastdate"));
vo.setFlag(rs.getInt("flag")); allAdmins.add(vo);
} return allAdmins;
}

然后将该链表转换成内存中的XML结构:

    public static Document getDocument( List<Admin> allAdmins) throws Exception
{
DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = dbfactory.newDocumentBuilder();
Document document = documentBuilder.newDocument(); Element root = document.createElement("admins"); for(Admin item : allAdmins)
{
Element admin = document.createElement("admin");
admin.setAttribute("aid", item.getAid()); Element rid = document.createElement("rid");
rid.appendChild(document.createTextNode(String.valueOf(item.getRid()))); Element type = document.createElement("type");
type.appendChild(document.createTextNode(String.valueOf(item.getType()))); Element lastdate = document.createElement("lastdate");
lastdate.appendChild(document.createTextNode(String.valueOf(item.getLastdate()))); Element flag = document.createElement("flag");
flag.appendChild(document.createTextNode(String.valueOf(item.getFlag()))); admin.appendChild(rid);
admin.appendChild(type);
admin.appendChild(lastdate);
admin.appendChild(flag); root.appendChild(admin);
} document.appendChild(root); return document;
}

然后将该内存的XML保存到本地中:

    public static void main(String[] args) throws Exception
{
List<Admin> allAdmins = loadFromDB();
Document document = getDocument(allAdmins); Source source = new DOMSource(document);
StreamResult stream = new StreamResult(new FileOutputStream(new File("C:\\D\\code\\resource\\admins.xml")));
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer trans = transFactory.newTransformer();
trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
trans.transform(source, stream); System.out.println("main done//~~");
}

也可以用下面代码把xml文件加载到内存中:

     public static Document loadFromFile(String path) throws Exception
{
Document document = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(new File(path));
return document;
}
上一篇:[Java学习]常用类-包装类型


下一篇:Android 解析XML文件和生成XML文件