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;
然后使用绝对路径
这个方法解决所有路径问题。。建议使用这个
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
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;
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 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;
}
}
保存XML文件的一个类, 使用如下。:String path = this.getServlet().getServletContext().getRealPath(
"congress\\exportdata\\");
得到文件路径+文件名:
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) { String path = this.getServlet().getServletContext().getRealPath(
"congress\\exportdata\\");
ExportForm exprotForm = (ExportForm) form;
exportButton = exprotForm.getExportButton(); button = request.getParameter("button");
fileName = request.getParameter("fileName"); DataExportToXML dataExport = new DataExportToXML(); if (button != null) {
filePath = path + "\\" + fileName;
dataExport.deleteFile(filePath); }
if (exportButton != null) { String fileName = "" + System.currentTimeMillis();
filePath = path + "\\" + fileName + ".xtdb";
try {
dataExport.DataExportToFile(filePath);
} catch (IOException e) {
mapping.findForward("exception");
e.printStackTrace();
}
} List list = null;
Resources aaa = new Resources();
list = dataExport.getFiles(path, ".xtdb");
SplitPagePara paraList = new SplitPageParaSon();
paraList.setData(list);
Iterator ite = list.iterator();
while (ite.hasNext()) {
FileInformation fileinfo = (FileInformation) ite.next();
}
request.getSession().setAttribute("exportFiles", paraList);
return mapping.findForward("success");
}
}