JDOM使用示例

虽然前面的文章提到了JDOM在解析XML文件方面存在种种缺陷,但至少在一般的小文件来说还是可以考虑的,比如我的项目中用到的XML文件就很小很小

这里只需添加个jdom.jar包就够了,至于是哪个版本自己可以上网去下

TargetExcel.xml

<?xml version="1.0" encoding="gbk"?>
<linekong>
   <project name="CRM2001">
		<property name="x">0</property>
		<property name="y">2</property>
	</project>
	<project name="CRM2002">
		<property name="x">0</property>
		<property name="y">3</property>
	</project>
	<project name="CRM2003">
		<property name="x">0</property>
		<property name="y">4</property>
	</project>
	<project name="CRM3004">
		<property name="x">0</property>
		<property name="y">5</property>
	</project>
	<project name="CRM3005">
		<property name="x">0</property>
		<property name="y">6</property>
	</project>
	<project name="CRM3002">
		<property name="x">0</property>
		<property name="y">7</property>
	</project>
	<project name="CRM3003">
		<property name="x">0</property>
		<property name="y">8</property>
	</project>
	
	
	<project name="CRM4002">
		<property name="x">0</property>
		<property name="y">9</property>
	</project>
	<project name="CRM4008">
		<property name="x">0</property>
		<property name="y">10</property>
	</project>
	<project name="CRM4007">
		<property name="x">0</property>
		<property name="y">11</property>
	</project>
	<project name="CRM4005">
		<property name="x">0</property>
		<property name="y">12</property>
	</project>
	<project name="CRM4006">
		<property name="x">0</property>
		<property name="y">13</property>
	</project>
	<project name="CRM4001">
		<property name="x">0</property>
		<property name="y">14</property>
	</project>
	<project name="CRM3006">
		<property name="x">0</property>
		<property name="y">15</property>
	</project>
	
	<project name="TAG0001_01">
		<property name="x">0</property>
		<property name="y">16</property>
	</project>
	<project name="TAG0001_02">
		<property name="x">0</property>
		<property name="y">17</property>
	</project>
	<project name="TAG0002">
		<property name="x">0</property>
		<property name="y">19</property>
	</project>
	<project name="TAG0003">
		<property name="x">0</property>
		<property name="y">20</property>
	</project>
	<project name="TAG0004">
		<property name="x">0</property>
		<property name="y">21</property>
	</project>
	<project name="TAG0005">
		<property name="x">0</property>
		<property name="y">22</property>
	</project>
	<project name="TAG0006">
		<property name="x">0</property>
		<property name="y">23</property>
	</project>
	<project name="TAG0007">
		<property name="x">0</property>
		<property name="y">24</property>
	</project>
	<project name="TAG0008">
		<property name="x">0</property>
		<property name="y">25</property>
	</project>
	<project name="TAG0009">
		<property name="x">0</property>
		<property name="y">26</property>
	</project>
	<project name="TAG0010">
		<property name="x">0</property>
		<property name="y">27</property>
	</project>
	<project name="TAG0011">
		<property name="x">0</property>
		<property name="y">28</property>
	</project>
	<project name="TAG0012">
		<property name="x">0</property>
		<property name="y">29</property>
	</project>
	<project name="TAG0013">
		<property name="x">0</property>
		<property name="y">30</property>
	</project>
	<project name="TAG0014">
		<property name="x">0</property>
		<property name="y">31</property>
	</project>
	<project name="TAG0015">
		<property name="x">0</property>
		<property name="y">32</property>
	</project>
	<project name="TAG0016">
		<property name="x">0</property>
		<property name="y">33</property>
	</project>
	<project name="TAG0017">
		<property name="x">0</property>
		<property name="y">34</property>
	</project>
	<project name="TAG0018">
		<property name="x">0</property>
		<property name="y">35</property>
	</project>
	<project name="TAG0019">
		<property name="x">0</property>
		<property name="y">36</property>
	</project>
	<project name="TAG0020">
		<property name="x">0</property>
		<property name="y">37</property>
	</project>
	<project name="TAG0021">
		<property name="x">0</property>
		<property name="y">38</property>
	</project>
	
</linekong>

对应处理程序

 

package com.cn.util.common;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import com.cn.pojo.TargetExcelPojo;
import com.cn.util.timer.TestTimerTaskServlet;

/**
 * @author meisong
 * 这里用到的是JDOM技术
 */
public class PropertiesCache {
	private final static Logger log = org.apache.log4j.Logger
			.getLogger(PropertiesCache.class);
	private  Log logger = LogFactory.getLog(PropertiesCache.class);
	private Map cache;
	private static PropertiesCache instance = new PropertiesCache();

	public PropertiesCache() {
		cache = Collections.synchronizedMap(new HashMap());// �õ�һ��ͬ����HashMap
	}

	//���õ�̬
	public static PropertiesCache getInstance() {
		return instance;
	}

	public String getProperties(String projectName, String propertiesName) {
		String key = projectName + propertiesName;
		if (cache.containsKey(propertiesName)) {
			String properties = (String) cache.get(key);
			return properties;
		} else {
			//
			String properties = parseXML(projectName, propertiesName);
			cache.put(key, properties);
			return properties;
		}
	}

	private String parseXML(String projectName, String propertiesName) {
		logger.info("解析XML文件");
		SAXBuilder sb = new SAXBuilder();
		Document doc = null;
		try {
			// String home = System.getenv("TOMCAT_HOME");
			// System.out.println(home);

			// 读取应用配置文件
			// 用于读取同步指标数据
 			URL urlAppCfg = PropertiesCache.class.getClassLoader().getResource(
					"AppConfig.xml");
			doc = sb.build(urlAppCfg);
 			 System.out.println(doc.toString()+"---");
		} catch (IOException e) {
			log.error(e.getMessage() + "--");
		} catch (JDOMException e) {
			log.error(e.getMessage() + "--ww");
		}
		String value = null;
		Element root = doc.getRootElement();
		List projects = root.getChildren("project");
	
		for (int i = 0; i < projects.size(); i++) {
			Element project = (Element) projects.get(i);
			if (project.getAttributeValue("name").equals(projectName)) {
				List properties = project.getChildren("property");
				for (int j = 0; j < properties.size(); j++) {
					Element property = (Element) properties.get(j);
					if (property.getAttributeValue("name").equals(
							propertiesName)) {
						value = property.getText();
						break;
					}
				}
				break;
			}
		}
		return value;
	}


}

 

你可能感兴趣的:(apache,tomcat,log4j,xml,cache)