dom4j解析xml
@RequestMapping("/getGridColumn") public void getGridColumn(HttpServletRequest request,HttpServletResponse response,String model) throws Exception{ //xml存在web-inf目录下 String filePath = request.getSession().getServletContext().getRealPath("/WEB-INF")+"/columnName.xml"; Map<String, Object> map = new HashMap<String, Object>(); SAXReader reader = new SAXReader(); Document doc = reader.read(new File(filePath)); Element root = doc.getRootElement(); Iterator<?> iters = root.elementIterator("column"); while(iters.hasNext()){ Element column = (Element)iters.next(); //取得column节点的名称 String columnName = column.attributeValue("name"); if(columnName!=null && columnName.equals(model)){ List<Map<String, Object>> list = new ArrayList<Map<String,Object>>(); map.put("className", columnName); map.put("isMultiple", column.attributeValue("isMultiple")); Iterator<?> items = column.elementIterator("item"); //设置行号 Map<String, Object> columnNumber = new HashMap<String, Object>(); columnNumber.put("text", "行号"); columnNumber.put("xtype", "rownumberer"); columnNumber.put("align", "center"); columnNumber.put("width", 40); list.add(columnNumber); while(items.hasNext()){ Element item = (Element)items.next(); Map<String, Object> itemMap = new HashMap<String, Object>(); itemMap.put("dataIndex",item.attributeValue("name")); itemMap.put("text",item.attributeValue("value")); int width = 128; if(item.attributeValue("width")!=null && !item.attributeValue("width").equals("")){ width = Integer.parseInt(item.attributeValue("width")); } if(item.attributeValue("hidden")!=null && !item.attributeValue("hidden").equals("")){ if(item.attributeValue("hidden").equals("true")){ itemMap.put("hidden",true); } } List<Map<String, Object>> renderersList = new ArrayList<Map<String,Object>>(); @SuppressWarnings("unchecked") List<Element> renderers = item.elements("renderer"); if(renderers!=null && renderers.size()>0){ for(Element renderer : renderers){ Map<String, Object> rendererMap = new HashMap<String, Object>(); rendererMap.put("name",renderer.attributeValue("name")); rendererMap.put("value",renderer.attributeValue("value")); if(renderer.attributeValue("color")!=null && !renderer.attributeValue("color").equals("")){ rendererMap.put("color",renderer.attributeValue("color")); } renderersList.add(rendererMap); } } itemMap.put("width",width); itemMap.put("align","center"); if(renderersList.size()>0){ itemMap.put("renderers",renderersList); } list.add(itemMap); } map.put("columns", list); //如果找到和Model匹配的名称则跳出循环 break; } } Gson gson = new Gson(); outPrint.printSting(request, response,true, gson.toJson(map)); }