JSP讀寫XML檔案

來源:互聯網
上載者:User

<%@ page contentType="text/html;charset=utf-8" %>
<%@ page import = "java.io.*,java.util.*,com.hexun.wap.zgpack.*,org.jdom.*,org.jdom.input.*,org.jdom.output.*;" %>
<html>
<head>
<title>Basic News Page</title>
<style type="text/css"><!--td {font-size:12px}--></style>
</head>
<body>
<%
 String t = request.getParameter("t")!=null?request.getParameter("t"):"";
 String s = request.getParameter("s")!=null?request.getParameter("s"):"";
 String d = request.getParameter("detail")!=null?request.getParameter("detail"):"";
 String delete = request.getParameter("delete")!=null?request.getParameter("delete"):"";
 String id = request.getParameter("id")!=null?request.getParameter("id"):"";
 SAXBuilder sb = new SAXBuilder();
 String indent = " ";
 boolean newLines = true;
 XMLOutputter outp = new XMLOutputter (indent,newLines,"gb2312");
 String flag = request.getParameter("flag")!=null?request.getParameter("flag"):"false";
 if(flag.equals("true"))
 {
  byte[] b = t.getBytes("iso8859-1");
  String ti = new String(b,"utf-8");
  byte[] b2 = s.getBytes("iso8859-1");
  String si = new String(b2,"utf-8");
  byte[] b3 = d.getBytes("iso8859-1");
  String di = new String(b3,"utf-8");
  try
         {
              Document doc=sb.build(new FileInputStream("/usr/local/stockdata/basic.xml"));
              Element root=doc.getRootElement();
              List list=root.getChildren();
              Element article = new Element("message");
              article.setAttribute("id",(list.size()+10001)+"");
              article.addContent(new Element("title").setText(ti));
              article.addContent(new Element("content").setText(di));
              article.addContent(new Element("time").setText(si));
              doc.getRootElement().addContent(article);
              outp.output(doc,new FileOutputStream("/usr/local/stockdata/basic.xml"));
         }
         catch(Exception e)
         {
              e.printStackTrace();
         }
  s = "";
  d = "";
  t = "";
 }
 if(delete.equals("true"))
 {
  new XMLFile().removeContent("/usr/local/stockdata/basic.xml",Integer.parseInt(id));
 }
%>
<form action="OperBasic.jsp" method="post">
<table width=80% border=0 bgcolor=#FFE4C4 cellspacing=1 cellpadding=1 align=center>
<tr><td colspan=2>Add Basic News :</td></tr>
<tr>
<td>Title:</td><td><input type="text" name="t" value="<%=t%>" maxlength="60" format="*N" style="border-width:1; border-color:black; border-style:solid;"/></td></tr>
<tr><td>Time/Author:</td><td><input type="text" name="s" value="<%=s%>" maxlength="20" format="*N" style="border-width:1; border-color:black; border-style:solid;"/></td></tr>
<tr><td>Content:</td><td><textarea name="detail" rows="10" cols="66" style="border-width:1; border-color:black; border-style:solid;"></textarea><br/> 
</td>
<tr><td colspan=2 align=center><input type="submit" name="submit" value="Add"/></td>
<input type="hidden" name="flag" value="true" maxlength="20" format="*N"/>
</tr>
</table>
</form>
<table width=80% border=0 bgcolor=000000 cellspacing=1 cellpadding=1 align=center><tr><td bgcolor=FF7Dff>ID</td><td bgcolor=FF7Dff>Title</td><td bgcolor=FF7Dff>Content</td><td bgcolor=FF7Dff>Time / Author</td><td bgcolor=FF7Dff>Delete</td></tr>
<%
 try
 {
  Document doc = sb.build(new FileInputStream("/usr/local/stockdata/basic.xml"));
  Element root = doc.getRootElement();
  List list = root.getChildren();
  out.println("all:"+list.size());
             for(int i=0;i<list.size();i++)
             {
   Element item = (Element)list.get(i);
   String name = item.getAttribute("id").getValue();
   out.println("<tr><td width=5% height=30 align=left bgcolor=FF7D00>"+name+"</td>");
   Element sub = item.getChild("title");
   String text = sub.getText();
   out.println("<td width=10% height=30 align=left bgcolor=FF7D00>"+ text+"</td>");
   Element sub2 = item.getChild("content");
   String text2 = sub2.getText();
   out.println("<td width=50% align=left bgcolor=FF7D00>"+ text2+"</td>");
   Element sub3 = item.getChild("time");
   String text3 = sub3.getText();
   out.println("<td width=10% align=left bgcolor=FF7D00>"+ text3+ "</td>");
   out.println("<td width=5% align=left bgcolor=FF7D00><a href=OperBasic.jsp?delete=true&id="+ i +" onclick=/"{if(confirm("+"'are you sure?'"+")){return true;}return false;}/">Delete</a></td>");
   //out.println("<td width=5% align=left bgcolor=FF7D00><a href=OperBasic.jsp?delete=true&id="+ i + ">Delete</a></td>");
   out.println("</tr>");
             }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

%>
</table>
<br/>
</body>
</html>
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.