Java and XML (iii) Read and write applications on the Web

Source: Internet
Author: User

Using the two functions I wrote in (ii) (put in package src), I implemented the Web page operation this time.

Index.html:
<%@ page language= "java" pageencoding= "GB2312"%>
<body>
<p></p>
<p></p>
<p></p>
<table width= "60%" border= "1" align= "Center" >
<tr>
<td>
<p align= "left" ><font size= "4" color= "#003399" > Management test Program </font></p>
<form name= "Readform" Method=post action= "load.jsp" >
<p align= "left" ><font color= "#FF0000" > Read XML file: </font></p>
<p align= "Left" > <font color= "#FF0000" >
<input type= "text" name= "MyPath" size= "" value= "C:eclipseworkspacemyxmlxmldata1.xml" >
</font></p>
<p align= "Left" >
<a href= "http://j5c.cnblogs.com/index.php#" Onclick=submit () > Read </a>
</p>
<p align= "left" ></p>
</form>
<form name= "WriteForm" Method=post action= "create.jsp" >
<p align= "left" ><font color= "#FF0000" > Write to XML file:</font> </p>
<p align= "Left" > Please fill in the path:
<input type= "text" name= "MyPath" width= "50" size= "
Value= "C:eclipseworkspacemyxmlxmldata11.xml" >
</p>
<p align= "Left" > Please fill in the title:
<input type= "text" name= "MyTitle" width= "M" size= ">"
</p>
<p align= "Left" > Please fill in the content:
<textarea name= "mycontent" cols= "></textarea>"
</p>
<p align= "Left" >
<a href= "http://j5c.cnblogs.com/index.php#" Onclick=submit () > Write </a>
</p>
</form>
</td>
</tr>
</table>
</body>

Page create.jsp to write:

<%@ page language= "java" pageencoding= "GB2312"%>
<%@ page import= "src.*"%>
<%@ page import= "org.w3c.dom.*"%>
<%@ page import= "javax.xml.parsers.*"%>
<%@ page import= "javax.xml.transform.*"%>
<%@ page import= "Javax.xml.transform.dom.DOMSource"%>
<%@ page import= "Javax.xml.transform.stream.StreamResult"%>
<%@ page import= "java.io.*"%>
<body>
<%
String Mypath= (String) request.getparameter ("MyPath");
String mytitle= (String) request.getparameter ("MyTitle");
String mycontent= (String) request.getparameter ("mycontent");
Mypath=new String (mypath.getbytes ("iso-8859-1"), "GB2312");
Mytitle=new String (mytitle.getbytes ("iso-8859-1"), "GB2312");
Mycontent=new String (mycontent.getbytes ("iso-8859-1"), "GB2312");
try{
WriteXml myxml=new WriteXml (mypath);
Myxml.towrite (mytitle,mycontent);
Myxml.tosave ();
Out.print ("Your writing is successful.");
}
catch (parserconfigurationexception exp) {
Exp.printstacktrace ();
Out.print ("Your writing is failed.");
}
%>
</body>

Read the XML page load.jsp:

<%@ page language="java" pageEncoding="GB2312"%>
<%@ page import="src.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.Vector" %>
<%@ page import="javax.xml.parsers.*" %>
<%@ page import="org.w3c.dom.*" %>
<body>
<%
Vector A=new Vector();
String mypath=(String)request.getParameter("mypath");
out.println(mypath);%>
<p>
<%
readxml my = new readxml();
A = my.toRead(mypath);
for (int i = 0; i < A.size(); i++) {
out.println(A.elementAt(i));
%>
<p>
<%
}
%>
</body>

There is also a flaw in the write program, which simply creates XML format and content, rather than overwriting existing files.

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.