Java與XML(三)web上的讀寫應用

來源:互聯網
上載者:User
web|xml 利用(二)中我寫的兩個函數(放在package src中),這次實現web頁面的操作。
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">管理測試程式</font></p>

<form name="readform" method=post action="load.jsp">
<p align="left"><font color="#FF0000">讀取xml檔案: </font></p>
<p align="left"> <font color="#FF0000">
<input type="text" name="mypath" size="50" value="c:\\eclipse\\workspace\\myxml\\xmldata\\1.xml">
</font></p>
<p align="left">
<a href="#" onClick=submit()>讀取</a>
</p>
<p align="left"> </p>
</form>

<form name="writeform" method=post action="create.jsp">
<p align="left"><font color="#FF0000">寫入xml檔案:</font> </p>
<p align="left">請填寫路徑:
<input type="text" name="mypath" width="100" size="50"
value="c:\\eclipse\\workspace\\myxml\\xmldata\\11.xml">
</p>
<p align="left">請填寫題目:
<input type="text" name="mytitle" width="100" size="50">
</p>

<p align="left">請填寫內容:
<textarea name="mycontent" cols="100"></textarea>
</p>
<p align="left">
<a href="#" onClick=submit()>寫入</a>
</p>
</form>
</td>
</tr>
</table>
</body>

寫入的頁面create.jsp:
<%@ 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.*"%>
<html>
<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>
</html>


讀取xml的頁面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.*" %>
<html>
<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>
</html>
這個寫程式還有一個缺陷,它只是建立xml格式和內容,而不是改寫已有檔案。
如果您寫出了改寫檔案的程式望能交流。




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.