下面的内容,能实现从xml文件中读取数据,但是新的留言,回复之类的写不到xml中,怎么写?
说仔细点,一共有四个文件,别看下面长,复制下即可,希望谁帮忙调试下,我不知道问题在哪,求助,例如用到什么包之类的,也可给点提示lword.java:
package com.mayflower.lword;import org.xml.sax.*; 
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;public class lword
{
    private static Document doc=null;
    private String strCurrentId = null;
    
    public lword(){}
    
    public void setDocument(Document doc)
    {
        this.doc=doc;
    } //下面的方法是向文档树中插入新的元素的方法
public void insertElement(String strRootID, String strUserName, String strTitle, String strContent )
{
Element oRoot;
if( null == strRootID || 0 == strRootID.length()) //如果是新发表文章
      oRoot = doc.getDocumentElement();
else   //如果是回复文章
     oRoot = (Element)doc.getElementsByTagName( strRootID ).item(0).getParentNode(); insert(oRoot, strUserName, strTitle, strContent);
} //实际的插入工作在这格方法内完成
public void insert( Element oRoot, String strUserName, String strTitle, String strContent)
{
 Element username = doc.createElement("username");
 Text username_t= doc.createTextNode(strUserName);
 username.appendChild(username_t);  Element title = doc.createElement("title");
 Text title_t = doc.createTextNode(strTitle);
 title.appendChild(title_t);  Element content = doc.createElement("content");
 Text content_t = doc.createTextNode(strContent);
 content.appendChild(content_t);
         //利用当前时间的微秒计时生成ID标识
 Element oId = doc.createElement("id"+String.valueOf( System.currentTimeMillis()));  Element oNoteItem = doc.createElement("noteitem");
 oNoteItem.appendChild(oId);
 oNoteItem.appendChild(username);
 oNoteItem.appendChild(title);
 oNoteItem.appendChild(content);
 oRoot.appendChild( oNoteItem);
} //下面的方法输出文档信息
public void put(Writer out, Element oTarget)
{
try{
if( null == oTarget) return;
NodeList oChildList = oTarget.getChildNodes();
if( null == oChildList)
{
out.write("Error!");
return;
}
int ChildCount = oChildList.getLength(); if( oTarget.getNodeName().equals("notepad") || oTarget.getNodeName().equals("noteitem"))
{
if( oTarget.getNodeName().equals("noteitem")) out.write("<li>");
out.write("<ul>");
for( int i = 0 ; i < ChildCount ; i ++)
{
if( oChildList.item(i) instanceof Element)
{
put(out, (Element)oChildList.item(i));
}
}
out.write("</ul>");
if( oTarget.getNodeName().equals("noteitem")) out.write("</li>");
}
else if( oTarget.getNodeName().indexOf("id") == 0 )
{
strCurrentId = oTarget.getNodeName();
}
else if( oTarget.getNodeName().equals("username")|| oTarget.getNodeName().equals("content"))
{
out.write("<li>");
if(oTarget.getNodeName().equals("username"))
out.write("用户名为:");
else
out.write("内容为:");
NodeList oTextList = oTarget.getChildNodes();
for( int i = 0 ; i < oTextList.getLength() ; i ++ )
{
out.write( ((Text)oTextList.item(i)).getData());
}
out.write("</li>");
}
else if( oTarget.getNodeName().equals("title"))
{
out.write("<li>标题为:<a href='postform.jsp?reply="+strCurrentId+"'>");
NodeList oTextList = oTarget.getChildNodes();
for( int i = 0 ; i < oTextList.getLength() ; i ++ )
{
out.write( ((Text)oTextList.item(i)).getData());
}
out.write("</a></li>");
}
}catch(Exception e)
{
e.printStackTrace(System.err);
}
}
}lword.xml:
<?xml version="1.0" encoding="GB2312"?>
<!--My Little BBS-->
<notepad>     
  <noteitem>         
     <id1023777212382/>
     <username>guan</username>
     <title>欢迎使用</title>
     <content>欢迎大家光临我店</content>
        <noteitem>             
           <id1023777227213/>
           <username>cc</username>
           <title>呵呵</title>
           <content>这个网店还不错。</content>         
              <noteitem>
                 <id1023799686167/>                 
                 <username>chenyan</username>
                 <title>一般般</title>
                 <content>一般般</content>                        
              </noteitem>         
        </noteitem>
  </noteitem> 
  <noteitem>         
     <id1023777239020/>
     <username>chenyan</username>        
     <title>建议</title>
     <content>界面还可以再完善一些,增加修改和删除功能。</content>     
  </noteitem> 
  <noteitem>
     <id1023799946121/>
     <username>cc</username>
     <title>测试文章</title>
     <content>guan辛苦了</content>
  </noteitem>
</notepad>
lword.jsp:
<html>
<head>
<title>瑞士军刀包店留言板</title>
</head>
<%@page contentType="text/html;charset=GB2312"%>
<%@page import="java.io.*,javax.xml.parsers.*,org.w3c.dom.*"%>
<jsp:useBean id="lword" class="com.mayflower.lword.lword" />
<body bgcolor="#CFF1E1">
<center><h2>瑞士军刀包店留言板 </h2></center>
<a href="postform.jsp">发表留言</a><br>
<%
Document doc=null;
File xmlFile = new File(request.getRealPath("/") + "lword.xml");//下面是获取客户端请求信息的部分
String strUserName = request.getParameter("username");  
String strTitle = request.getParameter("title"); 
String strContent = request.getParameter("content"); 
String strPost = request.getParameter("post");  
String strReply = request.getParameter("reply");  //检查XML文档是否存在
if(doc==null)
{
//如果文档不存在,则使用解析器解析生成
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setIgnoringComments(true);
        dbf.setIgnoringElementContentWhitespace(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
try{
      doc=db.parse("file:///" + request.getRealPath("/") + "lword.xml");
}catch(Exception e ){
      out.println(e.toString());
}
}
lword.setDocument(doc);
if(strPost!=null)
{
   //如果发表文章
if( strUserName == null || strUserName.length()==0 )
out.println("<font color='red'><h3>用户名不能为空!<a href=\"javascript:history.back();\">重试</a></h3></font>");
else if( strTitle == null || strTitle.length()==0 )
out.println("<font color='red'><h3>标题不能为空!<a href=\"javascript:history.back();\">重试</a></h3></font>");
else if( strContent == null || strContent.length()==0 )
out.println("<font color='red'><h3>文章内容不能为空!<a href=\"javascript:history.back();\">重试</a></h3></font>");
else{
   strUserName=new String(strUserName.getBytes("iso8859-1"), "gb2312");
   strTitle=new String(strTitle.getBytes("iso8859-1"), "gb2312");
   strContent=new String(strContent.getBytes("iso8859-1"), "gb2312");
lword.insertElement( strReply, strUserName, strTitle, strContent);
    lword.put(out,doc.getDocumentElement() ); 
   }
}else{
  lword.put(out,doc.getDocumentElement());
}
%>
<a href="index.jsp">返回</a> 
</body>
</html>postform.jsp:
<html>
<head>
<title>发表或回复</title>
</head>
<%@page contentType="text/html;charset=GB2312"%>
<body bgcolor="#CFF1E1">
<%
   String strReply=request.getParameter("reply");
   String strPost=request.getParameter("post");
%>
<%if(strReply!=null)
  {%>
     <center>
     <h3>正在回复文章</h3>
     </center>
<%}else{%>
     <center>
     <h3>正在发表文章</h3>
     </center>
<%}%>
<form method="post" action="lword.jsp" name="fr">
<table width="60%" align="center" border="0">
<tr>
  <td>登录名:</td>
  <td><input name="username"></td>
</tr>
<tr>
  <td>话题:</td>
  <td><input name="title"></td>
</tr>
<tr>
  <td colspan="2">内容:</td>
</tr>
<tr>
  <td colspan="2">
    <textarea name="content" rows="5" cols="45"></textarea>
  </td>
</tr>
<tr>
  <td colspan="2" align="center">
    <a href="javascript:fr.submit();">发表</a>
    <a href="javascript:fr.reset();">重写</a>
  </td>
</tr>
<%
   if(strReply!=null)
   {%>
     <input type="hidden" name="reply" value="<%=strReply%>">
 <%}%>
  <input type="hidden" name="post" value="<%=strPost%>">
</table>
</form>
</body>
</html>