前两天项目经理给我个问题让我玩,在jsp程序中动态生成xml网站配置程序,大概的模样是
Common.xml
../cn/index.jsp
http://wangrunxin
../personal/more.jsp?type=1
../enterprise/more.jsp?type=1
../index/index.jsp
../index/index.jsp
这个common.xml中重复的节点是动态的,单独的节点是静态的,研究了很久,也找了很多资料,
现在把心得给大家分享一下o(∩_∩)o…
首先们建立这么一个common.jsp
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%>
common2.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="gbk"%>
<%@ page import="cn.CCmingzhou.jsp.Chinese,test.common,java.util.*" %>
"http://www.w3.org/TR/html4/loose.dtd">
common.java
/*
* common.java
*
* Created on 2007年11月6日, 上午10:02
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package test;
import java.util.ArrayList;
import org.dom4j.*;
import org.dom4j.io.*;
import java.io.*;
import java.util.Iterator;
import java.util.List;
/**
*冷漠大神 qq 361619004
* @author Angel
*/
public class common {
/** Creates a new instance of common */
public common() {
}
/**
*创建common.xml文件
*
*/
public void createCommon(List lanmu,List warning,
String toppage_advt,String web_head,
String foot_company,String foot_tel, String foot_email,String foot_copyright,
String bgcolor,String background,
List friendship,List websub)
{
String path=null;
path=this.getClass().getResource("/").toString();
path=path.substring(6,path.length()-8)+"common.xml";
//System.out.println("love "+path);
Document document=DocumentHelper.createDocument();
Element page=document.addElement("page");
Element item=page.addElement("item");
for(int i=0;i
String qian="";
String hou="";
String zong=(String)lanmu.get(i);
int zz=zong.indexOf("@");
qian=zong.substring(0,zz);
hou=zong.substring(zz+1);
Element lanmu2=item.addElement("lanmu");
Element module=lanmu2.addElement("module");
module.setText(qian);
Element superlink=lanmu2.addElement("superlink");
superlink.setText(hou);
}
for(int i=0;i
String qian="";
String hou="";
String zong=(String)warning.get(i);
int zz=zong.indexOf("@");
qian=zong.substring(0,zz);
hou=zong.substring(zz+1);
Element lanmu2=item.addElement("warning");
Element module=lanmu2.addElement("manual");
module.setText(qian);
Element superlink=lanmu2.addElement("link");
superlink.setText(hou);
}
Element toppage=page.addElement("toppage");
Element advt=toppage.addElement("advt");
advt.setText(toppage_advt);
Element webhead=page.addElement("webhead");
Element title=webhead.addElement("title");
title.setText(web_head);
Element foot=page.addElement("webfoot");
Element company=foot.addElement("company");
company.setText(foot_company);
Element tel=foot.addElement("tel");
tel.setText(foot_tel);
Element email=foot.addElement("email");
email.setText(foot_email);
Element copyright=foot.addElement("copyright");
copyright.setText(foot_copyright);
Element bg=page.addElement("bgcolor");
Element bgv=bg.addElement("value");
bgv.setText(bgcolor);
Element back=page.addElement("background");
Element backv=back.addElement("value");
backv.setText(background);
Element youq=page.addElement("friendship");
for(int i=0;i
int s=i+1;
Element linkto=youq.addElement("linkto"+s);
String qian="";
String hou="";
String zong=(String)friendship.get(i);
int zz=zong.indexOf("@");
qian=zong.substring(0,zz);
hou=zong.substring(zz+1);
Element home=linkto.addElement("home");
home.setText(qian);
Element homepage=linkto.addElement("homepage");
homepage.setText(hou);
}
Element sub=page.addElement("websub");
for(int i=0;i
int aa=i+1;
String qian="";
String hou="";
String zong=(String)websub.get(i);
int zz=zong.indexOf("@");
qian=zong.substring(0,zz);
hou=zong.substring(zz+1);
Element sub2=sub.addElement("sub"+aa);
sub2.setText(qian);
Element link=sub.addElement("link"+aa);
link.setText(hou);
}
/*
StringWriter sw = new StringWriter();
XMLWriter writer = null;
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("GBK");
try {
writer = new XMLWriter(format);
writer.setWriter(sw);
writer.write(document);
} catch (Exception ex) {
ex.printStackTrace();
}
*/
try{
XMLWriter output = new XMLWriter(
new FileWriter( new File(path)));
output.write( document );
output.close();
//System.out.println(path);
}
catch(IOException e){System.out.println(e.getMessage());}
}
public static void main(String args[])
{
List lanmu=new ArrayList();
lanmu.add("首页@index.jsp");
lanmu.add("新闻中心@news.jsp");
lanmu.add("图片管理@images.jsp");
lanmu.add("体育频道@tiyu.jsp");
lanmu.add("帮助@help.jsp");
lanmu.add("新闻@love.jsp");
List warning=new ArrayList();
warning.add("服务声明@fuwu.jsp");
warning.add("联系我们@wangrunxin.jsp");
warning.add("更多友情连接@lianjie.jsp");
String toppage_advt="images/wanjie_banner.gif";
String web_head="中铁十三局集团第五工程有限公司财务部";
String foot_company="中铁十三局集团第五工程有限公司财务部";
String foot_tel="电话:传真:邮编:130033 ";
String foot_email="[email protected]";
String foot_copyright="地址:长春市 吉ICP备05002347号";
String bgcolor="#ffffff";;
String background="#ffffff";
List youq=new ArrayList();
youq.add("银河公司@http://www.yhweb.com");
youq.add("大连IT人才网@http://www.dlithr.com");
youq.add("中华英才网@http://www.chinahr.com");
List websub=new ArrayList();
websub.add("药品招商@../comm/index.jsp");
websub.add("生产技术@../jianjie/shengchanjishu.jsp");
websub.add("荣誉证书@../comm/index.jsp");
new common().createCommon(lanmu,warning,toppage_advt,web_head,foot_company,foot_tel,foot_email,foot_copyright,bgcolor,background,youq,websub);
}
}
这个执行之后会把common.xml生成到WEB-INF/目录,大家如果想看看结果的话那就自己把这些东西都考过去自己运行一下吧 o(∩_∩)o…
作者:高秋
网名:冷漠大神
Qq:361619004
Mail:[email protected]
Time:2007年11月9日星期五