DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> XML學習教程 >> XML詳解 >> Java與XML(三)WEB上的讀寫應用
Java與XML(三)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:eclipseworkspacemyxmlxmldata1.XML">
  </font></p>
  <p align="left">
  <a href="http://j5c.ddvip.com/index.PHP#" 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:eclipseworkspacemyxmlxmldata11.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="http://j5c.ddvip.com/index.PHP#" 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格式和內容,而不是改寫已有文件。


XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved