读取xml文件类一

public static void init(){
		String filename=BeifengMVCXMLFactory.class.getResource("/beifengmvc.xml").getFile();
		File file=new File(filename);
		SAXReader reader=new SAXReader();
		Document doc=null;
		try {
			doc=reader.read(file);
			Element root=doc.getRootElement();
			Element packagee=root.element("package");
			String defaultfilename=packagee.attributeValue("extends");
			getDefaultXMLInfo(defaultfilename);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private static void getDefaultXMLInfo(String filename){
		File file=new File(BeifengMVCXMLFactory.class.getResource("/"+filename+".xml").getFile());
		SAXReader reader=new SAXReader();
		BeifengMvc mvc=new BeifengMvc();
		List<InterceptorInfo> allinterceptor=null;
		Map<String, InterceptorStack> stackmap=null;
		String defaultstack="";
		try {
			Document doc=reader.read(file);
			Element root=doc.getRootElement();
			Element packages=root.element("package");
			List<Element> list=packages.elements();
			for(Element e:list){
				if(e.getName().equals("interceptor")){
					if(null==allinterceptor){
						allinterceptor=new ArrayList<InterceptorInfo>();
					}
					InterceptorInfo info=new InterceptorInfo();
					info.setInterceptorname(e.attributeValue("name"));
					info.setInterceptorclass(e.attributeValue("class"));
					//获得拦截器内的标签,从而配置Result属性
					List<Element> resultlist=e.elements();
					if(resultlist!=null){
						Map<String, Result> resultmap=new HashMap<String, Result>();
						for(Element resulte:resultlist){
							Result r=new Result();
							r.setResultname(resulte.attributeValue("name"));
							Attribute type=resulte.attribute("type");
							if(type!=null){
								r.setResulttype(type.getValue());
							}
							r.setResultjsp(resulte.getText());
							resultmap.put(r.getResultname(), r);
						}
						info.setResultmap(resultmap);
					}
					allinterceptor.add(info);
				}else if(e.getName().equals("interceptor-stack")){
					if(null==stackmap){
						stackmap=new HashMap<String, InterceptorStack>();
					}
					InterceptorStack stack=new InterceptorStack();
					stack.setStackname(e.attributeValue("name"));
					List<Element> refelement=e.elements();
					List<InterceptorInfo> interceptorlist=new ArrayList<InterceptorInfo>();
					for(Element ref:refelement){
						String refname=ref.attributeValue("name");
						for(InterceptorInfo info:allinterceptor){
							if(refname.equals(info.getInterceptorname())){
								interceptorlist.add(info);
								break;
							}
						}
					}
					stack.setInterceptorlist(interceptorlist);
					stackmap.put(stack.getStackname(), stack);
				}else if(e.getName().equals("defaultstack")){
					defaultstack=e.attributeValue("name");
				}
			}
			mvc.setDefaultstack(defaultstack);
			mvc.setAllinterceptor(allinterceptor);
			mvc.setStackmap(stackmap);
			ConstantMVC.DEFAULTMVCINFO=mvc;
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

 

你可能感兴趣的:(xml,mvc,String,init)