JSP reads and writes XML files

Source: Internet
Author: 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> <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> <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> <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>
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.