在jb导入javax.xml.parsers.*在类库中要配置那个jar文件阿?
解决方案 »
- 求助!!在java中,怎样把读到的数据库表的信息直接输出为一个xml格式的文件啊?急!!(不确定哪个库的哪张表)
- 客户端与EJB事务的问题,请教各位前辈了
- 怎么在服务器的java文件中得到服务器的绝对路径
- 关于Hibernate无关联多表查询的问题
- xfire服务端接收不到客户端发来对象的值
- 自动评卷问题
- 想不明白Axis2的一个问题
- 求struts的taglib的api地址
- 寻找java.ejb包,请发给我一个。
- 没有源文件还能编译
- 急救阿!老是说找不到javax.xml.*的包!
- 最近公司要装FIREWALL,才注意到好象 C/S 结构的程序不能在INTERNET上使用,但是另一套JAVA SWING的C/S程序却可以运行。
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import net.sf.hibernate.HibernateException;
import org.apache.xerces.dom.DocumentImpl;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import com.netec.congress.business.xml.DataToXML;
import com.netec.congress.model.dao.TbMeetingDAO;
import com.netec.congress.model.dao.TbSysAuditsortDAO;
import com.netec.congress.model.dao.TbSysClanDAO;
import com.netec.congress.model.dao.TbSysDegreeDAO;
import com.netec.congress.model.dao.TbSysMeetingtypeDAO;
import com.netec.congress.model.dao.TbSysNationDAO;
import com.netec.congress.model.dao.TbSysPeriodDAO;
import com.netec.congress.model.dao.TbSysVocationDAO;
import com.netec.congress.model.dao.TbSysVotezoneDAO;
import com.netec.congress.model.dao._RootDAO;public class DataImportOutput {
private static Document doc = null;
public DataImportOutput(Document document){
DataImportOutput.doc=document;
}
private String otputDOMToXMLString(Document doc) throws IOException {
String XMLString = "";
//建立dom的输出格式
OutputFormat format = new OutputFormat(doc);
StringWriter stringOut = new StringWriter();
XMLSerializer serial = new XMLSerializer(stringOut, format);
serial.asDOMSerializer();
serial.serialize(doc.getDocumentElement());
XMLString = stringOut.toString();
System.out.println(XMLString);
return XMLString;
}
private void saveXMLString(String XMLString, String fileName)
throws IOException {
File file = new File(fileName);
if (file.exists()) {
file.delete();
}
file.createNewFile();
if (file.canWrite()) {
FileWriter fileOut = new FileWriter(file);
fileOut.write(XMLString);
fileOut.close();
}
}
public ArrayList getFiles(String filePath){
ArrayList arrayList= new ArrayList(); //保存文件信息对像
FileInformation fileInfo = new FileInformation();//文件信息对像
File file=new File(filePath);//建立当前目录中文件的File对象
FiterXML fileNameFiter = new FiterXML(".xtdb"); //只显不扩展名为.xtdb的文件
File filelist[]=file.listFiles(fileNameFiter);//取得代表目录中所有文件的File对象数组
for(int i=0;i<filelist.length;i++){
if(filelist[i].isFile()){
fileInfo.setFileName(filelist[i].getName());
fileInfo.setCreatDate(new Date(filelist[i].lastModified()));
arrayList.add(fileInfo);
}
}
return arrayList;
} public void saveDOMToXML(String filePath, Document doc) throws IOException {
String XMLString = null;
XMLString = this.otputDOMToXMLString(doc); //把document转为字符串型
this.saveXMLString(XMLString, filePath); //保存为xml格式
}
public Element getTableDOM(String tableName, List rowsList) {
Element columNameElement=null;
Text columTextNode=null;
String columName = "";
String columValue = "";
Set set=null;
HashMap map=null;
DataToXML dataToXML=null;
Element row=null;
Iterator iterator=null;
Iterator rowIterator = rowsList.iterator();
Element table = doc.createElement("table"); //创建table结点
Attr attr = doc.createAttribute("tableName"); //创建table结点的属性结点tableName
attr.setValue(tableName); //设置属性结点的属性值为传入参数tableName
table.setAttributeNode(attr); //把属性结点添加到table结点上
while (rowIterator.hasNext()) {
row = doc.createElement("row");
dataToXML = (DataToXML) rowIterator.next();
map = dataToXML.getMap();//map中保存着表的所有字段名,及字段对应的值
set = map.keySet();
iterator = set.iterator();
while (iterator.hasNext()) {
columName = (String) iterator.next();
columValue = (String) map.get(columName);
columNameElement = doc.createElement(columName);
columTextNode = doc.createTextNode(columValue);
columNameElement.appendChild(columTextNode);
row.appendChild(columNameElement);
}
table.appendChild(row);
}
return table;
} public static void main(String args[]) {
List clanList = null;
List nationList=null;
List degreeList=null;
List auditSortList=null;
List meetingTypeList=null;
List periodList=null;
List vocationList=null;
List votezoneList=null;
List meetingList=null;
Element table = null;
try {
_RootDAO.initialize();
System.out.println("数据库初始化成功");
} catch (HibernateException e) {
System.out.println("数据库初始化失败");
} try {
TbSysClanDAO tbSysClanDAO = new TbSysClanDAO();
TbSysNationDAO tbSysNationDAO=new TbSysNationDAO();
TbSysDegreeDAO tbSysDegreeDAO =new TbSysDegreeDAO();
TbSysAuditsortDAO tbSysAuditsortDAO = new TbSysAuditsortDAO();
TbSysMeetingtypeDAO tbSysMeetingTypeDAO = new TbSysMeetingtypeDAO();
TbSysPeriodDAO tbSysPeriodDAO = new TbSysPeriodDAO();
TbSysVocationDAO tbSysVocationDAO = new TbSysVocationDAO();
TbSysVotezoneDAO tbSysVotezoneDAO = new TbSysVotezoneDAO();
TbMeetingDAO tbMeetingDAO = new TbMeetingDAO();
clanList = tbSysClanDAO.findAll();
nationList= tbSysNationDAO.findAll();
degreeList=tbSysDegreeDAO.findAll();
auditSortList=tbSysAuditsortDAO.findAll();
meetingTypeList=tbSysMeetingTypeDAO.findAll();
periodList=tbSysPeriodDAO.findAll();
vocationList=tbSysVocationDAO.findAll();
votezoneList=tbSysVotezoneDAO.findAll();
meetingList = tbMeetingDAO.findAll();
System.out.println("得到数据成功");
} catch (HibernateException e2) {
System.out.println("得到数据失败");
}
Document document=new DocumentImpl();
DataImportOutput output = new DataImportOutput(document);
Element tables = document.createElement("tables");
table = output.getTableDOM("tb_sys_clan", clanList);
tables.appendChild(table);
table =output.getTableDOM("tb_sys_nation",nationList);
tables.appendChild(table);
table=output.getTableDOM("tb_sys_degree",degreeList);
tables.appendChild(table);
table=output.getTableDOM("tb_sys_auditsort",auditSortList);
tables.appendChild(table);
table=output.getTableDOM("tb_sys_meetingtype",meetingTypeList);
tables.appendChild(table);
table=output.getTableDOM("tb_sys_period",periodList);
tables.appendChild(table);
table=output.getTableDOM("tb_sys_vaction",vocationList);
tables.appendChild(table);
table=output.getTableDOM("tb_sys_votezone",votezoneList);
tables.appendChild(table);
table=output.getTableDOM("tb_meeting",meetingList);
tables.appendChild(table);
document.appendChild(tables);
try {
output.saveDOMToXML("d:\\tttt.xtdb", document);
System.out.println("保XML保存成功");
ArrayList arrayList =output.getFiles("d:\\");
Iterator iter=arrayList.iterator();
while(iter.hasNext()){
FileInformation fi = (FileInformation)iter.next();
System.out.println(fi.getFileName());
System.out.println(fi.getCreatDate());
}
} catch (IOException e1) {
System.out.println("XML保存失败");
}
}
}