使用SAXReader对XML进行操作
该例子主要使用SAXReader对XML进行操作,browse.xml是Ango框架里面的XML文件
采用两种方法,第一种的全部是iterator,另外一种采用了部分的for each
代码如下
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属性如下: