日期:2014-05-18  浏览次数:20796 次

怎么把数据提交到xml文件
点提交把form内容(包括电话、联系地址、留言内容等)存入xml文件(包括电话、联系地址、留言内容等),请高手指点。

------解决方案--------------------
xml文件也是文本文件,和处理字符串的方法一样。
你在服务器端拼成xml格式的字符串,存到文件即可
------解决方案--------------------
可以用dom4j,根据提交的内容生成一个xml文件

public int createXMLFile(String filename){
int returnValue = 0;
Document document = DocumentHelper.createDocument();
Element booksElement = document.addElement( "books ");
booksElement.addComment( "This is a test for dom4j, holen,2004.9.11 ");
Element bookElement = booksElement.addElement( "book ");
bookElement.addAttribute( "show ", "yes ");
Element titleElement = bookElement.addElement( "title ");
titleElement.setText( "Dom4j Tutorials ");
bookElement = booksElement.addElement( "book ");
bookElement.addAttribute( "show ", "yes ");
titleElement = bookElement.addElement( "title ");
titleElement.setText( "Lucene Studing ");
bookElement = booksElement.addElement( "book ");
bookElement.addAttribute( "show ", "no ");
titleElement = bookElement.addElement( "title ");
titleElement.setText( "Lucene in Action ");
Element ownerElement = booksElement.addElement( "owner ");
ownerElement.setText( "O 'Reilly ");
try{
XMLWriter writer = new XMLWriter(new FileWriter(new File(filename)));
writer.write(document);
writer.close();
returnValue = 1;
}catch(Exception ex){
ex.printStackTrace();
}
return returnValue;
}

大概就是这么个样子,如果是插入已有的xml文件,做相应修改就好了