dom4j使用指南之一

关键字: dom4j, 配置, 使用
# 关键的API
SAXReader = new SAXReader();
is = this.getClass().getClassLoader().getResourceAsStream("beans.xml");
Document doc = reader.read(is);

Element root = doc.getRootElement();
List<?> subElements = root.elements("bean");
Element subElement = ele.element("bean");

Attribute idAttr = subElement.attribute("id");
String eleName = subElement.attributeValue("id");
eleName = idAttr.stringValue();

String beanClassName = subElement.attributeValue("class");
Object bean = Class.forName(beanClassName).newInstance();



Java 代码:

private Map<String, Object> parseBeanXml(InputStream is) throws Exception {
                SAXReader reader = new SAXReader();
                Document doc = reader.read(is);

                Element root = doc.getRootElement();
                logger.debug("root element name:" + root.getName());
                List<?> beanList = root.elements("bean");
                List<Element> beanElements = new ArrayList<Element>();
                for (Object beanObj : beanList) {
                        Element bean = (Element) beanObj;
                        String beanName = bean.attributeValue("id");
                        String beanClassStr = bean.attributeValue("class");
                        Class<?> beanClass = Class.forName(beanClassStr);
                        Object beanInstance = beanClass.newInstance();
                        beanElements.add(bean);
                        beanFactory.put(beanName, beanInstance);
                }

                for (Element bean : beanElements) {
                        List<?> proxyElements = bean.elements("proxy");
                        if (!proxyElements.isEmpty()) {
                                String beanName = bean.attributeValue("id");
                                String beanClassStr = bean.attributeValue("class");
                                Class<?> beanClass = Class.forName(beanClassStr);
                                List<Class<?>> proxyClasses = new ArrayList<Class<?>>();
                                List<Interceptor> interceptors = new ArrayList<Interceptor>();
                                for (Object proxyObj : proxyElements) {
                                        Element proxy = (Element) proxyObj;
                                        Attribute proxyRefAttr = proxy.attribute("ref");
                                        Class<?> proxyClass = beanFactory.get(
                                                        proxyRefAttr.getStringValue()).getClass();
                                        proxyClasses.add(proxyClass);
                                        interceptors.add((Interceptor) proxyClass.newInstance());
                                }

                                Object beanProxy = getProxy(beanClass.newInstance(),
                                                interceptors);
                                beanFactory.put(beanName, beanProxy);
                        }
                }
                return this.beanFactory;
        }

你可能感兴趣的:(bean,xml)