如果我输入了数据库设计,我想通过 java 代码生成一个 xml 模式。在下面的代码中,我从用户那里提取了输入数据库,我想生成给定数据库模式的 xml。
public class Table
{
private ArrayList<Column> columns;
private String NAME;
private static int count=0;
public Table()
{
columns = new ArrayList<Column>();
NAME=new String();
this.NAME="Table"+count;
count++;
}
public void AddColumn(Column column)
{
for(Column c: columns)
{
if(c.getname().equals(column.getname())){return;}
}
columns.add(column);
}
public void SaveToFile() throws IOException
{
FileWriter fw = new FileWriter("C:/Users/Ashad/Desktop/text.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("\r\n");
bw.write(this.NAME);
bw.write("\r\n");
for(Column c: columns)
{
bw.write("\r\n");
bw.write(c.getname()+" ");
bw.write(c.getdatatype()+" ");
if(c.getPK()== true)
{
bw.write("true"+" ");
}
else
{ bw.write("false"+" ");
}
if(c.getNN()== true)
{
bw.write("true"+" ");
}
else
{bw.write("false"+" "); }
}
bw.close();
}
public boolean DeleteColumn(String name)
{
for (Column c : columns)
{
if (c.getname().equals(name)) {return columns.remove(c);}
}
return false;
}
public void viewColumns()
{
System.out.append(NAME+" ");
for(Column c: columns)
{
System.out.append("\n");
System.out.append(c.getname()+" ");
System.out.append( c.getdatatype()+" ");
if(c.getPK()== true)
{
System.out.print(true);
}
else
System.out.print(false);
}
}
public String getNAME() {
return NAME;
}
}
public class Column
{
private String name;
private String dataType;
private boolean PK;
private boolean NN;
public Column()
{
String name=new String();
String dataType=new String();
boolean PK=false;
boolean NN=false;
}
public String getname(){return name;}
public String getdatatype(){return dataType;}
public boolean getPK(){return PK;}
public boolean getNN(){return NN;}
public void setname(String n){name=n;}
public void setdatatype(Object object){dataType=(String) object;}
public void setPK(boolean p){PK=p;}
public void setNN(boolean n){NN=n;}
}