xml处理工具包使用

需求 :生成动态数据源配置文件为列


package com.bky.datasource;

import java.util.List;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

@Root(name="beans")
public class MutilDataSourceXmlConfig {

	@Attribute(name="xmlns")
	private String xmlns = "http://www.springframework.org/schema/beans";
	
	@Attribute(name="xmlns:xsi")
	private String xmlnsxsi = "http://www.w3.org/2001/XMLSchema-instance";
	
	@Attribute(name="xsi:schemaLocation")
	private String xsischemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd";
	
	
	@ElementList(entry = "bean", inline = true,required = true)
	private List<MutilDataSourceXmlConfigBean> beans ;


	public String getXmlns() {
		return xmlns;
	}


	public void setXmlns(String xmlns) {
		this.xmlns = xmlns;
	}


	public String getXmlnsxsi() {
		return xmlnsxsi;
	}


	public void setXmlnsxsi(String xmlnsxsi) {
		this.xmlnsxsi = xmlnsxsi;
	}


	public String getXsischemaLocation() {
		return xsischemaLocation;
	}


	public void setXsischemaLocation(String xsischemaLocation) {
		this.xsischemaLocation = xsischemaLocation;
	}


	public List<MutilDataSourceXmlConfigBean> getBeans() {
		return beans;
	}


	public void setBeans(List<MutilDataSourceXmlConfigBean> beans) {
		this.beans = beans;
	}
	
	
	
	
}


package com.bky.datasource;

import java.util.List;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.ElementArray;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

@Root
public class MutilDataSourceXmlConfigBean {

	@Attribute(name="id")
	private String id ;
	
	@Attribute(name="class")
	private String clazz ;
	
	@Attribute(name="destroy-method")
	private String destroyMethod ="close";
	
	@Attribute(name="lazy-init")
	private boolean lazyInit = true;
	
	@ElementList(entry = "property", inline = true , required= true)
	private List<MutilDataSourceXmlConfigBeanProperty> properties ;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getClazz() {
		return clazz;
	}

	public void setClazz(String clazz) {
		this.clazz = clazz;
	}

	public String getDestroyMethod() {
		return destroyMethod;
	}

	public void setDestroyMethod(String destroyMethod) {
		this.destroyMethod = destroyMethod;
	}

	public boolean isLazyInit() {
		return lazyInit;
	}

	public void setLazyInit(boolean lazyInit) {
		this.lazyInit = lazyInit;
	}

	public List<MutilDataSourceXmlConfigBeanProperty> getProperties() {
		return properties;
	}

	public void setProperties(List<MutilDataSourceXmlConfigBeanProperty> properties) {
		this.properties = properties;
	}
	
	
	
}


package com.bky.datasource;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Root;

@Root
public class MutilDataSourceXmlConfigBeanProperty {

	@Attribute(name="name")
	private String name ;
	
	@Attribute(name="value")
	private String value ;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
	
	
}



package cn.richinfo.cmail.dbrouter;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

public class MutilDataSourceXmlConfigUtil {
	
	
	public static void createXml(MutilDataSourceXmlConfig config , 
			Map<MutilDataSourceXmlConfigBean, Map<String, String>> maps, String path,ContextHolder handdler) throws Exception{

			Set<String> set = new HashSet<String>() ;
		    for (MutilDataSourceXmlConfigBean key : maps.keySet()) {
		    	set.add(key.getId()) ;
		    }
			handdler.init(set) ;
			MutilDataSourceXmlConfig configXml = createMutilDataSourceXmlConfig(config,maps,handdler);
			Serializer serializer = new Persister();
			serializer.write(configXml, new File(path));
	}
	
	public static MutilDataSourceXmlConfig createMutilDataSourceXmlConfig(MutilDataSourceXmlConfig config ,Map<MutilDataSourceXmlConfigBean, Map<String, String>> beansMap,ContextHolder handdler) {

		List<MutilDataSourceXmlConfigBean> beanList = new ArrayList<MutilDataSourceXmlConfigBean>();
		for (MutilDataSourceXmlConfigBean key : beansMap.keySet()) {
			//设置bean属性id
			key.setId(handdler.getValue(key.getId())) ;
			beanList.add(createMutilDataSourceXmlConfigBean(key, beansMap.get(key)));
		}
		config.setBeans(beanList) ;
		return config;
	}
/*
	public static void createXml(MutilDataSourceXmlConfig config ,MutilDataSourceXmlConfigBean bean , 
			Map<String, Map<String, String>> maps, String path,ContextHolder handdler) throws Exception{

			handdler.init(maps.keySet()) ;
			MutilDataSourceXmlConfig beans = createMutilDataSourceXmlConfig(config,bean, maps,handdler);
			Serializer serializer = new Persister();
			serializer.write(beans, new File(path));
	}
	
	public static MutilDataSourceXmlConfig createMutilDataSourceXmlConfig(MutilDataSourceXmlConfig beans ,MutilDataSourceXmlConfigBean bean ,Map<String, Map<String, String>> beansMap,ContextHolder handdler) {
		MutilDataSourceXmlConfig config = new MutilDataSourceXmlConfig();
		//构建xml属性
		config.setXmlns(beans.getXmlns()) ;
		config.setXmlnsxsi(beans.getXmlnsxsi()) ;
		config.setXsischemaLocation(beans.getXsischemaLocation()) ;

		List<MutilDataSourceXmlConfigBean> beanList = new ArrayList<MutilDataSourceXmlConfigBean>();
		for (String key : beansMap.keySet()) {
			//设置bean属性id
			bean.setId(handdler.getValue(key)) ;
			beanList.add(createMutilDataSourceXmlConfigBean(bean, beansMap.get(key)));
		}
		beans.setBeans(beanList) ;
		return beans;
	}
*/
	public static MutilDataSourceXmlConfigBean createMutilDataSourceXmlConfigBean(MutilDataSourceXmlConfigBean bean,
			Map<String, String> propMap) {
		// 构建bean
		//MutilDataSourceXmlConfigBean bean = new MutilDataSourceXmlConfigBean();
		//bean.setId(param.getId());
		//bean.setClazz(param.getClazz());
		/*if (param.getDestroyMethod() != null) {
			bean.setDestroyMethod(param.getDestroyMethod());
		}
		bean.setLazyInit(param.isLazyInit());*/

		bean.setProperties(createMutilDataSourceXmlConfigBeanProperty(propMap));

		return bean;

	}

	public static List<MutilDataSourceXmlConfigBeanProperty> createMutilDataSourceXmlConfigBeanProperty(
			Map<String, String> map) {
		List<MutilDataSourceXmlConfigBeanProperty> proList = new ArrayList<MutilDataSourceXmlConfigBeanProperty>();
		MutilDataSourceXmlConfigBeanProperty property = null;
		for (String key : map.keySet()) {
			property = new MutilDataSourceXmlConfigBeanProperty();
			property.setName(key);
			property.setValue(map.get(key));
			proList.add(property);
		}

		return proList;
	}

}


main方法
/**
	 * 生成数据库路由bean配置文件
	 * @param list
	 * @param filePath
	 * @param ddrService
	 * @author wuchaohui
	 */
	public static void createRoutBeanXml(List<DbRouterModel> list, String filePath ,DbRouterDatasourcePropertyService ddrService) {

		try {
			Map<MutilDataSourceXmlConfigBean, Map<String, String>> maps = new HashMap<MutilDataSourceXmlConfigBean, Map<String, String>>();

			Map<String, String> map = null;
			
			for (int i = 0; i < list.size(); i++) {
				map = new HashMap<String, String>();
				DbRouterModel model = list.get(i);
				dealDbRouterModelNullAttribute(model) ;
				if (DbRouterConst.DYNAMIC_DATASOURCE_DRUID_TYPE.equals(model.getDbRouterDatasourceType())) {// druid
					List<DbRouterDatasourcePropertyModel> props = ddrService
							.searchList(model.getDbRouterDatasourceType());

					DruidDataSourceAttrModel attr = getDruidDataSourceAttrModel(props,model.getDbType(),model.getDbIp(),model.getDbPort(),model.getDbName(),model.getDbUser(),model.getDbPwd());

					map = BeanCopierUtils.getProperties(attr);
					
					MutilDataSourceXmlConfigBean bean = new MutilDataSourceXmlConfigBean() ;
					bean.setId(String.valueOf(model.getCorpId())) ;
					
					bean.setClazz(DbRouterConst.DYNAMIC_DATASOURCE_DRUID_NAME);
					bean.setDestroyMethod("close");
					bean.setLazyInit(true);
					
					maps.put(bean, map);
				}

			}

			ContextHolder handdler = new LogRouterContextHolder();
			MutilDataSourceXmlConfig configParam = new MutilDataSourceXmlConfig();

			MutilDataSourceXmlConfigUtil.createXml(configParam, maps, filePath, handdler);
		} catch (Exception e) {
			logger.error("cmd = HelpUtil:createRoutBeanXml | msg = 初始化生成日志懂爱数据库配置异常",e);
		}

	}




执行结果:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
   <bean id="ds_cmail_dbrouter_1002" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" lazy-init="true">
      <property name="testOnBorrow" value="false"/>
      <property name="testWhileIdle" value="true"/>
      <property name="validationQuery" value="SELECT &apos;x&apos;"/>
      <property name="password" value="2146230530"/>
      <property name="url" value="jdbc:mysql://192.168.34.143:3308/test_logreport?null"/>
      <property name="removeAbandonedTimeout" value="180"/>
      <property name="testOnReturn" value="false"/>
      <property name="maxActive" value="20"/>
      <property name="username" value="richmail"/>
      <property name="logAbandoned" value="true"/>
      <property name="maxOpenPreparedStatements" value="10"/>
      <property name="maxWait" value="60000"/>
      <property name="minIdle" value="3"/>
      <property name="removeAbandoned" value="true"/>
      <property name="timeBetweenEvictionRunsMillis" value="30000"/>
      <property name="minEvictableIdleTimeMillis" value="1800000"/>
      <property name="initialSize" value="1"/>
   </bean>
   <bean id="ds_cmail_dbrouter_1001" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" lazy-init="true">
      <property name="testOnBorrow" value="false"/>
      <property name="testWhileIdle" value="true"/>
      <property name="validationQuery" value="SELECT &apos;x&apos;"/>
      <property name="password" value="2146230530"/>
      <property name="url" value="jdbc:mysql://192.168.34.143:3308/test_logreport?null"/>
      <property name="removeAbandonedTimeout" value="180"/>
      <property name="testOnReturn" value="false"/>
      <property name="maxActive" value="20"/>
      <property name="username" value="richmail"/>
      <property name="logAbandoned" value="true"/>
      <property name="maxOpenPreparedStatements" value="10"/>
      <property name="maxWait" value="60000"/>
      <property name="minIdle" value="3"/>
      <property name="removeAbandoned" value="true"/>
      <property name="timeBetweenEvictionRunsMillis" value="30000"/>
      <property name="minEvictableIdleTimeMillis" value="1800000"/>
      <property name="initialSize" value="1"/>
   </bean>
   <bean id="ds_cmail_dbrouter_1000" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" lazy-init="true">
      <property name="testOnBorrow" value="false"/>
      <property name="testWhileIdle" value="true"/>
      <property name="validationQuery" value="SELECT &apos;x&apos;"/>
      <property name="password" value="2146230530"/>
      <property name="url" value="jdbc:mysql://192.168.34.143:3308/test_logreport?null"/>
      <property name="removeAbandonedTimeout" value="180"/>
      <property name="testOnReturn" value="false"/>
      <property name="maxActive" value="20"/>
      <property name="username" value="richmail"/>
      <property name="logAbandoned" value="true"/>
      <property name="maxOpenPreparedStatements" value="10"/>
      <property name="maxWait" value="60000"/>
      <property name="minIdle" value="3"/>
      <property name="removeAbandoned" value="true"/>
      <property name="timeBetweenEvictionRunsMillis" value="30000"/>
      <property name="minEvictableIdleTimeMillis" value="1800000"/>
      <property name="initialSize" value="1"/>
   </bean>
   <bean id="ds_cmail_dbrouter_1010" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" lazy-init="true">
      <property name="testOnBorrow" value="false"/>
      <property name="testWhileIdle" value="true"/>
      <property name="validationQuery" value="SELECT &apos;x&apos;"/>
      <property name="password" value="2146230530"/>
      <property name="url" value="jdbc:mysql://192.168.34.143:3308/test_logreport?null"/>
      <property name="removeAbandonedTimeout" value="180"/>
      <property name="testOnReturn" value="false"/>
      <property name="maxActive" value="20"/>
      <property name="username" value="richmail"/>
      <property name="logAbandoned" value="true"/>
      <property name="maxOpenPreparedStatements" value="10"/>
      <property name="maxWait" value="60000"/>
      <property name="minIdle" value="3"/>
      <property name="removeAbandoned" value="true"/>
      <property name="timeBetweenEvictionRunsMillis" value="30000"/>
      <property name="minEvictableIdleTimeMillis" value="1800000"/>
      <property name="initialSize" value="1"/>
   </bean>
</beans>

你可能感兴趣的:(simple-xml xml)