%@page contentType="text/html"
import="java.io.*,
javax.xml.parsers.*,
org.apache.xml.serialize.*,
org.w3c.dom.*" %>
<%!
Document oDoc = null;
XMLSerializer oSerializer = null;
File oDir = new File("d:/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("");
for( int i = 0 ; i < ChildCount ; i ++)
{
if( oChildList.item(i) instanceof Element)
{
put(out, (Element)oChildList.item(i));
}
}
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");
if( false == oDir.exists()) oDir.mkdir();
if( false == oXMLFile.exists())
{
DocumentBuilder oBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
oDoc = oBuilder.newDocument();
oXMLFile.createNewFile();
Element root = oDoc.createElement("notepad");
oDoc.appendChild(root);
insertElement2Tree( (String)null, "SysOp", "welcome", "congratulations");
saveTree();
put(out, root );
}
else
{
if( null == oDoc)
{
try
{
DocumentBuilder oBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
oDoc = oBuilder.parse(
new File(
request.getRealPath("/")+"Data/notepad.xml" ) );
}
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
{
out.flush();
%>
您正在回复
<%}
put(out, oDoc.getDocumentElement() );
}
%>