用java语言将数据库中的数据表转换为xml文件的通用程序
public void collectData(String namestring,int index,String type )
{
if(type.equals("attribute"))
attrilist.add(new SaveAttrName(namestring,index));
else
elelist.add(new SaveeleName(namestring,index));
//System.out.println("else");
//System.exit(0);
}
public void startWrite()
{
new WriteXmlFile(attrilist,elelist,rs,url).create(root,rootchild);
}
}
文件 SaveAttrName.java 内容:
package currencycreatexml;
class SaveAttrName
{
private String attributename;
private int index;
public SaveAttrName(String attributename,int index)
{
this.attributename=attributename;
this.index=index;
}
public String getAttributeName()
{
return attributename;
}
public int getIndex()
{
return index;
}
}
文件 SaveeleName.java 内容:
package currencycreatexml;
class SaveeleName
{
private String elementname;
private int index;
public SaveeleName(String elementname,int index)
{
this.elementname=elementname;
this.index=index;
}
public String getElementName()
{
return elementname;
}
public int getIndex()
{
return index;
}
}
文件 WriteXmlFile.java 内容:
package currencycreatexml;
import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
import javax.xml.parsers.*;
import org.w3c.dom.*; //使用dom解析
import org.apache.crimson.tree.*; //写xml文件需要用到的jar包
class WriteXmlFile
{
private ResultSet rs;
private String url;
private ArrayList attrilist;
private ArrayList elelist;
public WriteXmlFile(ArrayList attrilist,ArrayList elelist,ResultSet rs,String url)
{
this.attrilist=attrilist;
this.elelist=elelist;
this.rs=rs;
this.url=url;
}
/**
* @param root : xml文件的根元素名
*/
public void create(String root,String rootchild)
{
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
Document doc=null;
try
{
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.newDocument();
}
catch (ParserConfigurationException e)
{
e.printStackTrace();
}
Element rootelement=doc.createElement(root);
doc.appendChild(rootelement);
Iterator attri=attrilist.iterator();
Iterator ele=elelist.iterator();
//System.out.println("iterator");
try
{
while(rs.next())
{
Element rootchildelement=