用Java实现可保存状态的数据库生成XML树(2)

80酷酷网    80kuku.com

  xml|生成xml|数据|数据库4.3.构造生成XML的servlet
这一步的主要实现是从数据表中提取符合要求的记录生成嵌套的XML,servlet的源码是XMLServlet.java,选用oracle作数据库服务器。
对数据库记录处理的过程基本上类似于XSL模版的处理:
先查找layer的值为0的记录,对该记录进行分析,分析是否具有下级节点,这是通过该记录的href是否为空来进行分支处理的,href为空时,通过id的输入参数进行递归查找,请参见XMLServlet.java源码。

/*RowSet是ResultSet的增强类,具有可序列化的特点,所以代替ResultSet来作为可以放入SessionBean的结果集类型*/
import javax.sql.RowSet;
/*com.sitechasia.ejb.*包含需要的SessionBean的远程接口类*/
import com.sitechasia.ejb.*;
public class XMLServlet extends HttpServlet {
    PrintWriter out;
    private static ResultEdit re;
    private Object ref;
    boolean flag=true;
    private Handle handle=null;
    private static String HostIP = null;
        private static String HostPort = null;

        private static final String CONTENT_TYPEX = "text/xml";
        private static final String CONTENT_TYPEH = "text/html";
        /*不同的显示页面使用的XSL模版不一样,在这里先定义调用的XSL模版名称*/
        private static final String CONTENT_XSLT = "<?xml-stylesheet type=\"text/xsl\" href=\"/xsl/treefunc.xsl\"?>";
        private static final String CONTENT_XSLC = "<?xml-stylesheet type=\"text/xsl\" href=\"xmltreexsl.xsl\"?>";
        
        public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            try{
                /*获得客户端的session,判断该session里面是否含有SessionBean的句柄和其他相关内容,依据判断结果进行分支处理*/
                HttpSession session=request.getSession();
                
                    if  ( session.getAttribute("ResultEditHandle")==null) {
                        flag=false;
                    }
                    else {
                        flag=true;
                        /*获得保存在Session中SessionBean的句柄*/
                        handle=(Handle)session.getAttribute("ResultEditHandle");
                        /*从获得的句柄得到SessionBean的EJB实例*/
                        re =(ResultEdit)handle.getEJBObject();
                    }
                    /*session中含有SessionBean的分支处理*/
                    if (flag){
                        /*从SessionBean的实例中得到可序列化的结果集RowSet*/
                        RowSet rs = re.getRowSet();
                        response.setContentType(CONTENT_TYPEX);
                        out = response.getWriter();
                        out.println("<?xml version=\"1.0\" encoding=\"gb2312\" ?>");
                        /*通过判断Servlet是否带有request参数,来判断使用哪种模版*/
                        if (request.getParameterValues("clickId")==null){
                            out.println(CONTENT_XSLT);
                        }
                        else {
                            out.println(CONTENT_XSLC);
                        }
                        out.println("<project>");
                        /**使用递归算法按层次把结果集中的记录排列,这里没有使用结果集的各个字段名称来指定数据的取值,而是通过结果集的字段序号来取得相应的数据值的,所以需要把提交表单中查询语句按照这里规定的字段序号来书写,这样可以使有不同字段名称的不同的结果集按照一定顺序书写后,都可以用来生成XML树,在这里规定了字段序号的相对应的名称,在4.2.的数据表的结构定义中有详细描述,下面指出代码中指定的序号对应的模版字段名称:
id 1 表示序号,是节点的唯一标识;
             layer 2 层,顶层是从0开始计算,依此类推;
             name 3 节点的名称,也可以是节点的唯一识别标识;
             value 4 节点在浏览器中显示的名称,为用户提供一个快速查看
             father 5 父节点的id值,除开layer为0的节点该值为0;
             herf 6 链接对象,当一个节点是树叶时,存在相应的链接,树杈节点该值为空
target 7 链接对象显示的目标窗口,树杈节点该值亦为空
*/
                        while(rs.next()){
                            if (rs.getString(2).equals("0")){
                                if(rs.getString("herf")==null){
                                    out.println("<node>");
                                    out.println("<id>"+rs.getString(1)+"</id>");
                                    out.println("<layer>"+rs.getString(2)+"</layer>");
                                    out.println("<name>"+rs.getString(3)+"</name>");
                                    out.println("<value>"+(rs.getString(4))+"</value>");
                                    out.println("<father>"+rs.getString(5)+"</father>");
                                    out.println("<href></href>");
                                    out.println("<target></target>");
                                    childnode(rs.getString(1));
                                    out.println("</node>");
                                }
                                          else {
                                        out.println("<node>");
                                        out.println("<id>"+rs.getString(1)+"</id>");
                                        …
                                }
                            }
                        }
                        out.println("</project>");
                        rs.close();
                    }
                    else {
                        response.setContentType(CONTENT_TYPEH);
                        out = response.getWriter();
                        out.println("<html><head><title>no EJB message</title><META HTTP-EQUIV='Refresh' CONTENT='2;url=/RefreshPost.html'/></head>");
                        out.println("<body>");
                        out.println("<p><p><p><center>Refresh...... return RefreshPost.html</center></p></p></p></body></html>");
                    }
                }
                catch(Exception e){
                           System.out.println("errror:"+e.getMessage());
                    e.printStackTrace();
                }
        }
        
        /**Clean up resources
        */
        public void destroy() {
        }
        /**方法childnode(String father)是一个递归调用的方法,用来生成带有树层次关系的XML*/
        public void childnode(String father){
            try{
                    RowSet rs = re.getRowSet();
                    while(rs.next()){
                        if (rs.getString(5).equals(father)){
                            if(rs.getString("herf")==null){
                                out.println("<node>");
                                out.println("<id>"+rs.getString(1)+"</id>");
                                …
                                childnode(rs.getString(1));
                                out.println("</node>");
                            }
                            else{
                                out.println("<node>");
                                …
                                out.println("</node>");
                            }
                        }
                    }
                    rs.close();
                }
                catch(Exception e){
                    System.out.println(e.getMessage());
                    e.printStackTrace();
                }
        }
}



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: