我有一个本地 SQL 表,其中包含以下记录:
ID currentRank previousRank PlayerName Money
1 1 1 Max 15
2 2 2 Rick 20
3 3 3 Alice 13
4 4 4 Beth 25
我想将其转换为如下所示:
<player ID="1">
<currentRank>1</currentRank>
<previousRank>1</previousRank>
<PlayerName>Max</PlayerName>
<Money>15</Money>
</player>
<player ID="2">
<currentRank>2</currentRank>
<previousRank>2</previousRank>
<PlayerName>Rick</PlayerName>
<Money>20</Money>
</player>
<player ID="3">
<currentRank>3</currentRank>
<previousRank>3</previousRank>
<PlayerName>Alice</PlayerName>
<Money>13</Money>
</player>
<player ID="4">
<currentRank>4</currentRank>
<previousRank>4</previousRank>
<PlayerName>Alice</PlayerName>
<Money>13</Money>
</player>
我有一个名为 Tplayers 的类,它获取和设置所有字段,如下所示:
public class Tplayers implements Serializable
{
private int ID;
private String currentRank
private String previousRank
private String PlayerName
private String Money
}
public Tplayers () {
ID = 0;
currentRank = "";
previousRank = "";
PlayerName = "";
Money = "";
}
public Tplayers (int playerId, String currentRank, String previousRank, String PlayerName, String Money) {
this.playerId = playerId;
this.currentRank = currentRank;
this.previousRank = previousRank;
this.PlayerName = PlayerName;
his.Money = Money;
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String getCurrentRank() {
return currentRank;
}
public void setCurrentRank(String currentRank) {
this.currentRank = currentRank;
}
public String getPreviousRank() {
return previousRank;
}
public void setPreviousRank(String previousRank) {
this.previousRank = previousRank;
}
public String getPlayerName() {
return PlayerName;
}
public void setPlayerName(String PlayerName) {
this.PlayerName = PlayerName;
}
public String getMoney() {
return Money;
}
public void setMoney(String Money) {
this.Money = Money;
}
最后,我有 DbMethods 类,其中包含一个播放器 Arraylist,我将表中的所有数据存储在“播放器”对象中:
public static ArrayList<Tplayers > getAllPlayers() {
ArrayList<Tplayers > players = new ArrayList();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DBConnectionFactory.getConnection();
String sql = "SELECT * FROM players";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
Tplayers player = new Tplayers();
player.setID(rs.getInt("ID"));
player.setCurrentRank(rs.getString("CurrentRank"));
player.setPreviousRank(rs.getString("PreviousRank"));
player.setPlayerName(rs.getString("PlayerName"));
player.setMoney(rs.getString("Money"));
players.add(player);
}
return players;
} catch (SQLException e) {
System.err.println("Message: " + e.getMessage());
System.err.println("SQL State: " + e.getSQLState());
System.err.println("Error Code: " + e.getErrorCode());
return null;
} finally {
DBConnectionFactory.close(rs);
DBConnectionFactory.close(ps);
DBConnectionFactory.close(conn);
}
}
现在我想创建一个具有 main 方法的数据库类,该方法在执行时将创建一个类似于所描述的 XML 文件,但我不知道如何开始,我知道如何创建 XML 文档,但只能通过硬编码。非常感谢任何帮助,如果需要任何澄清,请随时询问。
此外,所有导入都在那里我只是没有将它们包含在帖子中以防止混乱发布后我将搜索其他站点以获取更多信息,因此上述代码可能会更改,我会在顶部记录任何更改。