該例子主要使用SAXReader對XML進行操做,browse.xml是Ango框架裏面的XML文件java
採用兩種方法,第一種的所有是iterator,另一種採用了部分的for eachsql
代碼以下session
private void doBrowse(ServletContextEvent sce) { HashMap<String,BrowseBean1> map1 = new HashMap<String,BrowseBean1>(); SAXReader saxReader = new SAXReader(); Document document = null; // xml文件位置 String path = this.getClass().getResource("/").getPath(); //String pString = this.getClass().getResource(pString).getPath(); String filePath = path.substring(0, path.length()- "classes/".length())+"browse.xml"; //logger.warn(path); //logger.warn(filePath); try { document = saxReader.read(new File(URLDecoder.decode(filePath, "utf-8"))); } catch (UnsupportedEncodingException e) { // 路徑中文解碼錯誤 e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); logger.warn("查詢操做的xml文檔異常"); } List list = document.selectNodes("/BrowseElements/element/@flag"); //logger.warn(list); Iterator iter = list.iterator(); /** * 本身寫的方法,裏面沒有所有使用迭代器,用的for each 循環 start */ while(iter.hasNext()){ Attribute attribute = (Attribute) iter.next(); String flag = attribute.getValue(); List listTemp1 = document.selectNodes("/BrowseElements/element[@flag='" + flag + "']/@pageSize"); Iterator iterTemp1 = listTemp1.iterator(); int pageSize = 0; while (iterTemp1.hasNext()) { Attribute attribute1 = (Attribute) iterTemp1.next(); pageSize = Integer.parseInt(attribute1.getValue()); } BrowseBean1 browseBean1 = new BrowseBean1(); browseBean1.setPageSize(pageSize); List listTemp2 = document.selectNodes("/BrowseElements/element[@flag='" + flag + "']/sql/@value"); LinkedHashMap<String,BrowseBean2> map2 = new LinkedHashMap<String,BrowseBean2>(); for(Object ob : listTemp2){ //System.out.println(ob); String value = ((Attribute) ob).getValue(); //System.out.println(value); BrowseBean2 browseBean2 = new BrowseBean2(); List listTemp3 = document.selectNodes("/BrowseElements/element[@flag='" + flag + "']/sql[@value='"+value+"']/@key"); for(Object ob1 : listTemp3){ String key = ((Attribute) ob1).getValue(); browseBean2.setKey(key); //System.out.println(key); } List listTemp4 = document.selectNodes("/BrowseElements/element[@flag='" + flag + "']/sql[@value='"+value+"']/struct"); ArrayList<StructBean> struct = new ArrayList<StructBean>(); for(Object ob2 : listTemp4){ StructBean structBean = new StructBean(); String structValueString = ((Element) ob2).getText(); String sessionString = ((Element) ob2).attributeValue("session"); String requestString = ((Element) ob2).attributeValue("request"); structBean.setStructValue(structValueString); structBean.setSession(sessionString); structBean.setRequest(requestString); struct.add(structBean); } browseBean2.setStruct(struct); List listTemp5 = document.selectNodes("/BrowseElements/element[@flag='" + flag + "']/sql[@value='"+value+"']/param"); ArrayList<ParamBean> param = new ArrayList<ParamBean>(); for(Object ob3 : listTemp5){ ParamBean paramBean = new ParamBean(); String paramValue = ((Element) ob3).getText(); String notNull = ((Element) ob3).attributeValue("notNull"); String session = ((Element) ob3).attributeValue("session"); String request = ((Element) ob3).attributeValue("request"); String drop = ((Element) ob3).attributeValue("drop"); String timeStart = ((Element) ob3).attributeValue("timeStart"); String timeEnd = ((Element) ob3).attributeValue("timeEnd"); paramBean.setParamValue(paramValue); paramBean.setNotNull(notNull); paramBean.setSession(session); paramBean.setRequest(request); paramBean.setDrop(drop); paramBean.setTimeStart(timeStart); paramBean.setTimeEnd(timeEnd); param.add(paramBean); //System.out.println(paramBean.getDrop()); if(((Element) ob3).getText()==null||"".equals(((Element) ob3).getText())){ System.out.println("該<element>paramBean爲空,即不須要參數"); //節點相似<param></param>,這樣空的才行,可是實際上若是不須要傳參數的話,根本就不用寫<param>這個節點,那這個判斷有點問題 }else{ System.out.println("paramValue爲:"+paramBean.getParamValue()); } } browseBean2.setParam(param); List listTemp6 = document.selectNodes("/BrowseElements/element[@flag='" + flag + "']/sql[@value='"+value+"']/title"); ArrayList<String> title = new ArrayList<String>(); for(Object ob4 : listTemp6){ String titleString = ((Element) ob4).getText(); title.add(titleString); //System.out.println(titleString); //System.out.println(title); } browseBean2.setTitle(title); List listTemp7 = document.selectNodes("/BrowseElements/element[@flag='" + flag + "']/sql[@value='"+value+"']/output"); ArrayList<String> output = new ArrayList<String>(); for(Object ob5 : listTemp7){ output.add(((Element) ob5).getText()); } browseBean2.setOutput(output); map2.put(value, browseBean2); } browseBean1.setMap(map2); map1.put(flag, browseBean1); System.out.println("遍歷完browse.xml中的一個<element></element>"); } /** * 本身寫的方法,裏面沒有所有使用迭代器,用的for each 循環 end */ /** * Ango框架寫法,所有使用iterator start */ while (iter.hasNext()) { Attribute attribute = (Attribute) iter.next(); String flag = attribute.getValue(); System.out.println(flag); List listTemp1 = document.selectNodes("/BrowseElements/element[@flag='" + flag + "']/@pageSize"); Iterator iterTemp1 = listTemp1.iterator(); int pageSize = 0; while (iterTemp1.hasNext()) { Attribute attribute1 = (Attribute) iterTemp1.next(); pageSize = Integer.parseInt(attribute1.getValue()); } BrowseBean1 browseBean1 = new BrowseBean1(); browseBean1.setPageSize(pageSize); List listTemp2 = document.selectNodes("/BrowseElements/element[@flag='" + flag + "']/sql/@value"); Iterator iterTemp2 = listTemp2.iterator(); LinkedHashMap<String,BrowseBean2> map2 = new LinkedHashMap<String,BrowseBean2>(); while (iterTemp2.hasNext()) { Attribute attribute2 = (Attribute) iterTemp2.next(); String value = attribute2.getValue(); BrowseBean2 browseBean2 = new BrowseBean2(); List listTemp3 = document.selectNodes("/BrowseElements/element[@flag='" + flag + "']/sql[@value='"+value+"']/@key"); Iterator iterTemp3 = listTemp3.iterator(); while (iterTemp3.hasNext()) { Attribute attribute3 = (Attribute) iterTemp3.next(); String key = attribute3.getValue(); browseBean2.setKey(key); } List listTemp4 = document.selectNodes("/BrowseElements/element[@flag='" + flag + "']/sql[@value='"+value+"']/struct"); Iterator iterTemp4 = listTemp4.iterator(); ArrayList<StructBean> struct = new ArrayList<StructBean>(); while (iterTemp4.hasNext()) { Element element = (Element) iterTemp4.next(); StructBean structBean = new StructBean(); structBean.setStructValue(element.getText()); structBean.setSession(element.attributeValue("session")); structBean.setRequest(element.attributeValue("request")); struct.add(structBean); } browseBean2.setStruct(struct); List listTemp5 = document.selectNodes("/BrowseElements/element[@flag='" + flag + "']/sql[@value='"+value+"']/param"); Iterator iterTemp5 = listTemp5.iterator(); ArrayList<ParamBean> param = new ArrayList<ParamBean>(); while (iterTemp5.hasNext()) { Element element = (Element) iterTemp5.next(); ParamBean paramBean = new ParamBean(); paramBean.setParamValue(element.getText()); paramBean.setNotNull(element.attributeValue("notNull")); paramBean.setSession(element.attributeValue("session")); paramBean.setRequest(element.attributeValue("request")); paramBean.setDrop(element.attributeValue("drop")); paramBean.setTimeStart(element.attributeValue("timeStart")); paramBean.setTimeEnd(element.attributeValue("timeEnd")); param.add(paramBean); } browseBean2.setParam(param); List listTemp6 = document.selectNodes("/BrowseElements/element[@flag='" + flag + "']/sql[@value='"+value+"']/title"); Iterator iterTemp6 = listTemp6.iterator(); ArrayList<String> title = new ArrayList<String>(); while (iterTemp6.hasNext()) { Element element = (Element) iterTemp6.next(); title.add(element.getText()); } browseBean2.setTitle(title); List listTemp7 = document.selectNodes("/BrowseElements/element[@flag='" + flag + "']/sql[@value='"+value+"']/output"); Iterator iterTemp7 = listTemp7.iterator(); ArrayList<String> output = new ArrayList<String>(); while (iterTemp7.hasNext()) { Element element = (Element) iterTemp7.next(); output.add(element.getText()); } browseBean2.setOutput(output); map2.put(value, browseBean2); } browseBean1.setMap(map2); map1.put(flag, browseBean1); } /** * Ango框架寫法,所有使用iterator end */ ServletContext sc = sce.getServletContext(); sc.setAttribute("adubBrowse", map1); logger.info("====================browse.xml已更新完畢===================="); }
XML結構以下:框架
<?xml version="1.0" encoding="UTF-8"?> <BrowseElements><!-- xmlns="http://www.w3school.com.cn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3school.com.cn browse.xsd" --> <element flag="browse_code" pageSize="100" description="獲取代碼表"> <!-- sql語句必須保證正確 --> <sql value="select * from code where `table` = ?"> <param>table</param> <title>代碼編號</title> <title>代碼內容</title> <title>權重1</title> <title>權重2</title> <title>備註</title> <output>id</output> <output>content</output> <output>weight1</output> <output>weight2</output> <output>note</output> <output>column</output> <output>table</output> </sql> </element> <element flag="browse_teacherInformation" pageSize="10" description="對教師進行查詢"> <!-- sql語句必須保證正確 --> <sql value="select * from teacher_info where id like ? and name like ? and college=? " description="瀏覽教師"> <param>id</param> <param>name</param> <param drop="true1" session="true">college</param> <title>職工號</title> <title>教師姓名</title> <title>性別</title> <title>所屬單位</title> <title>來校日期</title> <output>id</output> <output>name</output> <output>sex</output> <output>college</output> <output>arrive_time</output> </sql> </element> <element flag="browse_adminTeacherInformation" pageSize="10" description="對全部教師進行查詢"> <!-- sql語句必須保證正確 --> <sql value="select * from teacher_info where id like ? and name like ? and college like ? " description="瀏覽教師"> <param>id</param> <param>name</param> <param>college</param> <title>序號</title> <title>職工號</title> <title>教師姓名</title> <title>性別</title> <title>所屬單位</title> <title>來校日期</title> <output>id</output> <output>name</output> <output>sex</output> <output>college</output> <output>arrive_time</output> </sql> </element> <element flag="browse_teacherPassword" pageSize="10" description="對教師密碼進行模糊查詢"> <!-- sql語句必須保證正確 --> <sql value="select teacher_info.*,teacher_login.* from teacher_info ,teacher_login,admin_login where teacher_info.id=teacher_login.username and teacher_info.id like ? and teacher_info.name like ? and admin_login.username=? and teacher_info.college=? ORDER BY id DESC" description="查詢教師密碼"> <param>id</param> <param>name</param> <param session="true" drop="true">userName</param> <param session="true" drop="true">college</param> <title>教師姓名</title> <title>職工號</title> <title>性別</title> <title>密碼</title> <output>name</output> <output>id</output> <output>sex</output> <output>password</output> </sql> </element> <element flag="browse_administrator_teacherPassword" pageSize="10" description="對全部教師密碼進行模糊查詢"> <!-- sql語句必須保證正確 --> <sql value="select teacher_info.*,teacher_login.* from teacher_info ,teacher_login,admin_login where teacher_info.college like ? and teacher_info.id=teacher_login.username and teacher_info.id like ? and teacher_info.name like ? and admin_login.username=? ORDER BY id DESC" description="查詢全部教師密碼"> <param>college</param> <param>id</param> <param>name</param> <param session="true" drop="true">userName</param> <title>教師姓名</title> <title>職工號</title> <title>性別</title> <title>密碼</title> <title>所屬單位</title> <output>name</output> <output>id</output> <output>sex</output> <output>password</output> <output>college</output> </sql> </element> <element flag="browseNews" pageSize="10" description="對信息進行模糊查詢"> <!-- sql語句必須保證正確 --> <sql value="select * from news" description="信息查詢"> <param></param> <title>標題</title> <title>發佈時間</title> <title>審覈狀態</title> <output>title</output> <output>publishTime</output> <output>status</output> <output>id</output> </sql> </element> <element flag="getNews" pageSize="10" description="對信息進行模糊查詢"> <!-- sql語句必須保證正確 --> <sql value="select * from news where status = 1 limit 5" description="信息查詢"> <output>id</output> <output>title</output> <output>publishTime</output> </sql> </element> </BrowseElements>
各種javaBean屬性以下:this