java读取xml例子 SAXReader
用到的是DOM4J里面的SAXReader类,需要下载dom4j的jar包。
本文为了方便起见,特地在末尾上传了它的jar包。
?
此util类的功能是读取xml中的关于数据库配置的URL、用户名、密码。
util类如下:
?
?
package com.vastis.util;import java.io.File;import java.util.HashMap;import java.util.Iterator;import org.dom4j.Attribute;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.SAXReader;public class ReadSpringXmlUtil {public void iterateWholeXML(String filename, HashMap<String, String> hm) throws Exception{SAXReader saxReader = new SAXReader();try {Document document = saxReader.read(new File(filename));Element root = document.getRootElement();//遍历根结点,即第一层for (Iterator iter = root.elementIterator(); iter.hasNext();) {Element element = (Element) iter.next();// 获取id为dataSource的节点的beanif(element!=null){Attribute ageAttr = element.attribute("id");if (ageAttr != null && !ageAttr.equals("")) {String id = ageAttr.getValue();if (id != null && id.equals("dataSource")) {// 遍历第二层(即driverClassName,url,username,password),并进行处理for (Iterator iterInner = element.elementIterator(); iterInner.hasNext();) {Element elementInner = (Element) iterInner.next();if(elementInner!=null && !elementInner.equals("")){//获取name属性Attribute dsAttr = elementInner.attribute("name");if(dsAttr!=null && !dsAttr.equals(""))//下面开启遍历第3层,即4个propertyfor (Iterator iterThird = elementInner.elementIterator(); iterThird.hasNext();) {Element elementThird = (Element) iterThird.next();if(elementThird!=null && !elementThird.equals("")){if(elementThird.getName().equals("value")){hm.put(dsAttr.getValue(), elementThird.getText());//System.out.println("name="+elementThird.getName());//System.out.println("value="+elementThird.getText());}}}}}} } }}} catch (DocumentException e) {// TODO Auto-generated catch blocke.printStackTrace();throw new Exception(e);}}}?
?
调用util类的函数如下:将此函数放在main函数中即可跑起来。
?
?
/** * 读取数据库配置,从spring.conf.xml中读取 * @throws Exception */ private void configDBInfo() throws Exception{ HashMap<String, String> hashMap= new HashMap<String, String>(); ReadSpringXmlUtil rsxu=new ReadSpringXmlUtil(); rsxu.iterateWholeXML(FILE_PATH,hashMap); try{ if(hashMap!=null && hashMap.size()>0){ for(java.util.Iterator iter=hashMap.keySet().iterator();iter.hasNext();){ Object key=iter.next(); String value=hashMap.get(key); if(((String)key).equals(Constants.URL)){ ReadOnlyConfiger.setUrl(value); }else if(((String)key).equals(Constants.USERNAME)){ ReadOnlyConfiger.setUserName(value); }else if(((String)key).equals(Constants.PASSWORD)){ ReadOnlyConfiger.setPassword(value); } } }else{ System.err.println("数据库配置信息未找到,请查看spring.conf.xml文件中的数据配置信息是否存在!"); } }catch(Exception ex){ System.out.println(ex.toString()); throw new Exception(ex); } }?
?
xml源文件如下:
?
?
<?xml version="1.0" encoding="GB2312"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd"><beans> <!--JDBC数据源配置--><bean id="dataSource" abstract="true" abstract="true" parent="txProxyTemplate"> <property name="target"> <bean parent="txProxyTemplate"> <property name="target"> <bean parent="txProxyTemplate"> <property name="target"> <bean parent="txProxyTemplate"> <property name="target"> <bean parent="txProxyTemplate"> <property name="target"> <bean parent="txProxyTemplate"> <property name="target"> <bean parent="txProxyTemplate"> <property name="target"> <bean parent="txProxyTemplate"> <property name="target"> <bean parent="txProxyTemplate"> <property name="target"> <bean class="com.vastis.security.service.impl.SecurityFacadeImpl"> </bean> </property></bean></beans>?
refurl:http://hi.baidu.com/yuletianxia750/blog/item/920dae456c21fb8fb2b7dc90.html
?
?