发信人: huaronghu(润名)
整理人: zjxyz(2002-09-08 22:53:06), 站内信件
|
作者:润名
开发思路:建立一模板文件(Template.txt),生成 xml 文件时先读取些模板文件的数据,再取得表单提交的数据,然后才生成新的 xml 文件。
一、表单文件:index.xml (xslt 转换文件 toIndex.xsl,校验文件 index.dtd ,js 文件 myjs.js 以及样式表文件 common.css)
1、index.xml:
----------------------------
<?xml version="1.0" encoding="GB2312" standalone="no"?>
<!DOCTYPE index SYSTEM "index.dtd">
<?xml-stylesheet type="text/xsl" href="toIndex.xsl"?>
<index>
<formname>form</formname>
<formmethod>post</formmethod>
<formaction>/Jack/JavaBuildXml/post.xml</formaction>
<form>
<field color="#FF0000" id="name">
<fieldvalue>name</fieldvalue>
<fieldtype>text</fieldtype>
<fieldsize>20</fieldsize>
</field>
<field color="#0000FF" id="mail">
<fieldvalue>mail</fieldvalue>
<fieldtype>text</fieldtype>
<fieldsize>25</fieldsize>
</field>
<field color="#FF00FF" id="place">
<fieldvalue>place</fieldvalue>
<fieldtype>text</fieldtype>
<fieldsize>35</fieldsize>
</field>
<field onClick="javascript:checkform();">
<fieldvalue>submit</fieldvalue>
<fieldtype>button</fieldtype>
</field>
</form>
</index>
----------------------------------------------
2、toIndex.xsl:
<?xml version="1.0" encoding="GB2312"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<head>
<title>Form Information</title>
<LINK rel="stylesheet" type="text/css" href="common.css"></LINK>
<script language="javascript" src="myjs.js"></script>
</head>
<body bgcolor="#009999">
<xsl:for-each select="index">
<table width="100%" border="0" cellpadding="0" cellspacing="1">
<form>
<xsl:attribute name="name">
<xsl:value-of select="formname" />
</xsl:attribute>
<xsl:attribute name="method">
<xsl:value-of select="formmethod" />
</xsl:attribute>
<xsl:attribute name="action">
<xsl:value-of select="formaction" />
</xsl:attribute>
<xsl:for-each select="form/field">
<xsl:choose>
<xsl:when test="@id">
<tr>
<td width="15%" bgcolor="#FFFFFF">
<xsl:attribute name="color">
<xsl:value-of select="@color" />
</xsl:attribute>
<xsl:value-of select="@id" />:
</td>
<td width="78%" bgcolor="#FFFFFF">
<input>
<xsl:attribute name="name">
<xsl:value-of select="fieldvalue" />
</xsl:attribute>
<xsl:attribute name="type">
<xsl:value-of select="fieldtype" />
</xsl:attribute>
<xsl:attribute name="size">
<xsl:value-of select="fieldsize" />
</xsl:attribute>
</input>
</td>
</tr>
</xsl:when>
<xsl:otherwise>
<tr>
<td colspan="2" bgcolor="#FFFFFF" align="center">
<input>
<xsl:if test="@onClick">
<xsl:attribute name="onClick">
<xsl:value-of select="@onClick" />
</xsl:attribute>
</xsl:if>
<xsl:attribute name="value">
<xsl:value-of select="fieldvalue" />
</xsl:attribute>
<xsl:attribute name="type">
<xsl:value-of select="fieldtype" />
</xsl:attribute>
</input>
</td>
</tr>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</form>
</table>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
--------------------------------------------------------
3、index.dtd
<?xml version="1.0" encoding="ISO-8859-1"?>
<!ELEMENT index (formname, formmethod, formaction, form)>
<!ENTITY % field "fieldvalue, fieldtype, fieldsize?">
<!ENTITY % formProperty "#PCDATA">
<!ELEMENT formmethod (%formProperty;)>
<!ELEMENT formaction (%formProperty;)>
<!ELEMENT form (field)*>
<!ELEMENT field (%field;)>
<!ELEMENT fieldvalue (%formProperty;)>
<!ELEMENT fieldtype (%formProperty;)>
<!ELEMENT fieldsize (%formProperty;)>
<!ATTLIST field
color CDATA #IMPLIED
id CDATA #IMPLIED
onClick CDATA #FIXED "javascript:checkform();"
>
------------------------------------------------------
4、myjs.js
<!--
function checkform()
{
if (form.name.value == "" || form.mail.value == "" || form.place.value == "")
{
window.alert("The name,mail,place value isn't null.");
return false;
}
else
{
form.submit();
return true;
}
}
//-->
-------------------------------------------------------
5、common.css
td {font-size:14 pt;text-indent: 8 pt;}
-------------------------------------------------------------
------------------------------------------------------------------
二、Serlvet 文件,其功能是生成 xml 文件。代码如下:
package com.Servlet.JavaAndXml;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class JavaBuildXml extends HttpServlet
{
String EOL = System.getProperty("line.separator"); // 换行符
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
try
{
res.setContentType("text/html;charset=GBK");
PrintWriter out = res.getWriter();
String name = req.getParameter("name");
String mail = req.getParameter("mail");
String place = req.getParameter("place");
String sitePath = getSitePath(); // 取得站点的路径
String curPath = getCurrentPath(sitePath); //取得当前路径
String xmlfileAP = setXmlFileAccessPath(curPath); //设置 xml 文件的存放路径
createXmlAccessPath(xmlfileAP); // 建立 xml 文件存取目录
String curDate = getCurrentDate(); // 取得当前日期
String curTime = getCurrentTime(); // 取得当前 Time
StringBuffer sb = new StringBuffer();
sb.append(curDate);
sb.append(curTime);
sb.append(".");
sb.append("xml");
String xmlfile = sb.toString(); // 取得当前要建立的 xml 文件名
StringBuffer sb1 = new StringBuffer();
sb1.append(xmlfileAP);
sb1.append("\\");
sb1.append(xmlfile);
String xmlPath = sb1.toString(); // 取得当前要建立的 xml 文件的路径及文件名
String TemplateFile = getTemplateFile(curPath); // 取得模板文件的路径
String TempalteFileContent = getTemplateFileContent(TemplateFile); // 取得模板内容
boolean match = addxmlFileContent(name, mail, place, TempalteFileContent, xmlPath); // 生成所需要的 xml 文件
if (match)
{
out.println("The file has builded. <a href=\"http://localhost:7001/Jack/xml/xmlfile/"+ xmlfile +"\">visited....");
}
else
{
out.println("The file build failed. <a href=\"http://localhost:7001/Jack/xml/index.xml\">[Back]...");
}
out.flush(); // 刷新 out
out.close(); // 关闭 out
}
catch (Exception e)
{
System.out.println("JavaBuildXml: " + e.toString());
e.printStackTrace();
}
}
public String getSitePath()
{
// 取得站点的路径
String sitePath = String.valueOf(getServletConfig().getServletContext().getRealPath("/"));
return sitePath;
}
public String setXmlFileAccessPath(String curPath)
{
// 设置 xml 文件的存放路径,并返回
StringBuffer sb = new StringBuffer();
sb.append(curPath);
sb.append("\\").append("xmlfile");
String xmlfileAP = sb.toString();
return xmlfileAP;
}
public String getCurrentPath(String sitePath)
{
// 取得当前路径,并返回
StringBuffer sb = new StringBuffer();
sb.append(sitePath);
sb.append("\\").append("xml");
String curPath = sb.toString();
return curPath;
}
public void createXmlAccessPath(String xmlfileAP) throws Exception
{
// 建立 xml 文件存取目录
// 如果这个目录已经存在,则不建立
// 否则建立
try
{
File xmlDirectory = new File(xmlfileAP);
if (!xmlDirectory.exists())
{
xmlDirectory.mkdir();
}
}
catch(Exception ioe)
{
System.out.println(ioe.toString());
throw new Exception("createXmlAccessPath() method: " + ioe);
}
}
public String getCurrentDate()
{
// 取得当前日期,格式: year-month-day
Calendar currentDate = Calendar.getInstance();
StringBuffer sb = new StringBuffer();
sb.append(String.valueOf(currentDate.get(Calendar.YEAR))); // 年
sb.append(String.valueOf((new Integer(currentDate.get(Calendar.MONTH)).intValue())+1).toString()); // 月
sb.append(String.valueOf(currentDate.get(Calendar.DAY_OF_MONTH))); // 日
String curDate = sb.toString();
return curDate;
}
public String getCurrentTime()
{
// 取得当前的 Time
Date now = new Date();
String curTime = String.valueOf(now.getTime());
return curTime;
}
public String getTemplateFile(String curPath)
{
// 取得模板文件
StringBuffer sb = new StringBuffer();
sb.append(curPath);
sb.append("\\");
sb.append("Template.txt");
String TemplateFile = sb.toString();
return TemplateFile;
}
public String getTemplateFileContent(String TemplateFile) throws Exception
{
// 取得模板的内容
try
{
StringBuffer sb = new StringBuffer();
FileReader fr1 = new FileReader(TemplateFile);
BufferedReader br1 = new BufferedReader(fr1);
String TFileContent = "";
for (String line1="";(line1=br1.readLine())!=null;)
{
TFileContent = String.valueOf(TFileContent) + String.valueOf(String.valueOf(line1).concat(EOL));
}
br1.close();
fr1.close();
return TFileContent;
}
catch(Exception ex1)
{
System.out.println(ex1.toString());
throw ex1;
}
}
public boolean addxmlFileContent(String name, String mail, String place, String content, String path) throws Exception
{
try
{
content = content.concat("<detail>\r\n");
content = content.concat(" <field color=\"#FF0000\" id=\"name\">").concat(EOL);
content = content.concat(" <fieldvalue>"+ name +"</fieldvalue>").concat(EOL);
content = content.concat(" </field>").concat(EOL);
content = content.concat(" <field color=\"#0000FF\" id=\"mail\">").concat(EOL);
content = content.concat(" <fieldvalue>"+ mail +"</fieldvalue>").concat(EOL);
content = content.concat(" </field>").concat(EOL);
content = content.concat(" <field color=\"#FF00FF\" id=\"place\">").concat(EOL);
content = content.concat(" <fieldvalue>"+ place +"</fieldvalue>").concat(EOL);
content = content.concat(" </field>").concat(EOL);
content = content.concat("</detail>").concat(EOL);
FileWriter fw1 = new FileWriter(path);
BufferedWriter bw1 = new BufferedWriter(fw1);
bw1.write(content);
bw1.flush();
bw1.close();
fw1.close();
return true; // 如果操作成功,则返回 True
}
catch(Exception ex2)
{
System.out.println(ex2.toString());
return false; // 如果操作失败,则返回 false;
}
}
}
-----------------------------------------------------------------
-------------------------------------------------------------------
三、模板文件:Template.txt
<?xml version="1.0" encoding="GBK" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="../toDetail.xsl"?>
-------------------------------------------------------------
----------------------------------------------------------------
四、生成的 xml 文件 xslt 转换文件 toDetail.xsl,代码如下:
<?xml version="1.0" encoding="GBK"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<head>
<title>Detail Information</title>
<LINK rel="stylesheet" type="text/css" href="/Jack/xml/common.css"></LINK>
</head>
<body bgcolor="#009999">
<xsl:apply-templates select="detail" />
</body>
</html>
</xsl:template>
<xsl:template match="detail">
<table width="100%" border="0" cellpadding="0" cellspacing="1">
<xsl:for-each select="field">
<tr>
<td width="15%" bgcolor="#FFFFFF">
<xsl:attribute name="color">
<xsl:value-of select="@color" />
</xsl:attribute>
<xsl:value-of select="@id" />:
</td>
<td width="78%" bgcolor="#FFFFFF">
<xsl:value-of select="fieldvalue" />
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
------------------------------------------------
五、结论:功能很简陋,目的为了让初学者做进一步的了解,我在初学的时候碰到不少的问题,所以做这些文件的时候加了不少的注释,希望你们能尽快理解。
如有不清楚的地方,请与我联系:[email protected]
作者:润名 2002.08.24
----
|
|