Java与XML(三)web上的读写应用

王朝java/jsp·作者佚名  2006-01-09
宽屏版  字体: |||超大  

利用(二)中我写的两个函数(放在package src中),这次实现web页面的操作。

index.html:

<%@ page language="java" pageEncoding="GB2312"%>

<body>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</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">&nbsp;</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格式和内容,而不是改写已有文件。

如果您写出了改写文件的程序望能交流

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有