<%@page contentType="text/html" import="java.io.*, org.xml.sax.*, org.apache.xerces.parsers.DOMParser, org.apache.xerces.dom.*, org.apache.xml.serialize.*, org.w3c.dom.*" %> <%! DocumentImpl oDoc = null;//new DocumentImpl(); //初始化Document为null,这里使用null作为一个是否已经加载的标志 //DocumentTypeImpl oDT = new DocumentTypeImpl( oDoc, "http://cyfer.topcool.net/test", null, "notepad.dtd"); XMLSerializer oSerializer = null; File oDir = new File("/mytest/Data"); File oXMLFile = new File( oDir, "notepad.xml"); //下面的方法是保存文件的方法 void saveTree() { try { FileWriter oFileWriter = new FileWriter( oXMLFile); oSerializer = new XMLSerializer( oFileWriter, new OutputFormat( Method.XML , "GB2312" , true )); oSerializer.asDOMSerializer(); oSerializer.serialize( oDoc); oFileWriter.flush(); oFileWriter.close(); } catch(IOException e) { } } //下面的方法是向文档树中插入新的元素的方法 void insertElement2Tree(String strRootID, String strUserName, String strTitle, String strContent ) { Element oRoot; if( null == strRootID || 0 == strRootID.length()) oRoot = oDoc.getDocumentElement(); else oRoot = (Element)oDoc.getElementsByTagName( strRootID ).item(0).getParentNode(); insertElement2Tree(oRoot, strUserName, strTitle, strContent); } //插入方法的另一个版本,实际的插入工作在这格方法内完成 void insertElement2Tree( Element oRoot, String strUserName, String strTitle, String strContent) { Element uname = oDoc.createElement("uname"); Text uname_t= oDoc.createTextNode(strUserName); uname.appendChild(uname_t); Element title = oDoc.createElement("title"); Text title_t = oDoc.createTextNode(strTitle); title.appendChild(title_t); Element content = oDoc.createElement("content"); Text content_t = oDoc.createTextNode(strContent); content.appendChild(content_t); Element oId = oDoc.createElement("id"+String.valueOf( System.currentTimeMillis())); Element oNoteItem = oDoc.createElement("noteitem"); oNoteItem.appendChild(oId); oNoteItem.appendChild(uname); oNoteItem.appendChild(title); oNoteItem.appendChild(content); oRoot.appendChild( oNoteItem); } //下面的方法输出文档信息 String strCurrentId = null; 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("
  • "); out.write(""); if( oTarget.getNodeName().equals("noteitem")) out.write("
  • "); } else if( 0 == oTarget.getNodeName().indexOf("id") ) { strCurrentId = oTarget.getNodeName(); } else if( oTarget.getNodeName().equals("uname") || oTarget.getNodeName().equals("content")) { out.write("
  • "); if(oTarget.getNodeName().equals("uname")) 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("
  • "); } else if( oTarget.getNodeName().equals("title")) { out.write("
  • 标题为:"); NodeList oTextList = oTarget.getChildNodes(); for( int i = 0 ; i < oTextList.getLength() ; i ++ ) { out.write( ((Text)oTextList.item(i)).getData()); } out.write("
  • "); } } catch(IOException e) { } } %> <% //下面是获取客户端请求信息的部分 String strUserName = request.getParameter("uname"); String strTitle = request.getParameter("title"); String strContent = request.getParameter("content"); String strTarget = request.getParameter("replytarget"); String strObject = request.getParameter("uobject"); String strReply2 = request.getParameter("reply2"); //检查XML文档是否存在 if( false == oDir.exists()) oDir.mkdir(); if( false == oXMLFile.exists()) { //不存在则创建 oDoc = new DocumentImpl(); oXMLFile.createNewFile(); Element root = oDoc.createElement("notepad"); oDoc.appendChild(root); insertElement2Tree( (String)null, "SysOp", "welcome", "congratulations"); saveTree(); put(out, root ); } else { if( null == oDoc) { //如果文档不存在,则使用解析器进行文档解析 DOMParser oParser = new DOMParser(); try { oParser.parse( new InputSource( new FileReader( new File( request.getRealPath("/")+"Data/notepad.xml" )))); oDoc = (DocumentImpl)oParser.getDocument(); } catch(Exception e ) { //错误处理 out.println("Errer occured!"); out.println(e.toString()); out.println(request.getRealPath("/")); } } if( null == strReply2) { //下面几行非常懒惰,必须重写,记住 if( null == strUserName || 0 == strUserName.length() ) out.println(" uname is null!"); else if( null == strTitle || 0 == strTitle.length()) out.println(" title is null!"); else if( null == strContent || 0 == strContent.length() ) out.println(" content is null!"); else { //out.println(strUserName+" "+strTitle+" "+ strContent); insertElement2Tree( strObject, strUserName, strTitle, strContent); saveTree(); } } else {%>

    您正在回复

    <%} put(out, oDoc.getDocumentElement() ); } %>

    用户名:

    标题:

    内容:

    <% if( null != strReply2 ) {%> <%}%>