JSP读写XML文件

<%@ 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 +" οnclick=/"{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>
 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值