.dom4j(sax)读取spring的xml

.dom4j(sax)读取spring的xml
public void realxml(String fileName){
SAXReader  saxreader= new SAXReader();
Document document=null;
try{
   URL xmlpath = this.getClass().getClassLoader().getResource(fileName);
   document = saxreader.read(xmlpath);
   Map<String,String> nsMap = new HashMap<String,String>(); 
   nsMap.put("ns"," http://www.springframework.org/schema/beans");//加进命名空间
   XPath xsub = document.createXPath("//ns:beans/ns:bean");//创建beans/bean的查询路径
   xsub.setNamespaceURIs(nsMap);//设置命名空间
   List<Elements> beans= xsub.selectNodes(document);//获取的
   //文档所有的bean节点
  for(Elements element:beans){
                  String id= element.attributeValue("id");
                  String clazz=element.attributeValue("class");
                  BeanDfinition beanDefine = new BeanBefinition(id,clazz);
                   beanDefine.add(beanDefine);
    }
}
catch(Exception e){
e.printStackTrace();
}
}
//记得要建一个javabean(beanDefine)类来接受 id 和class (bean)

你可能感兴趣的:(.dom4j(sax)读取spring的xml)