java 读取xml文件

最近在写 一个 动态导入 Excel 的功能,其中利用了 dom4j 读取 xml 文件进行验证,poi 读取Excel 数据,reflect 反射生成 队对象,

接下来 先写java -dom4j 读取 xml 文件开始:

第一步:使用Dom4j开发,需下载dom4j相应的jar文件

        1.官网下载: http://www.dom4j.org/dom4j-1.6.1/

         2.dom4j是sourceforge.NET上的一个开源项目,因此可以到http://sourceforge.Net/projects/dom4j下载其最新版.

第二步:配置xml 文件用于读取

<?xml version="1.0" encoding="UTF-8"?>
<root>
	<!-- 规则信息:BusinessRule -->
	<improtExcel id="com.test.data.BusinessRule" name="ruleImport">				
		<mapping type="String" excelTitle="规则名称" property="name" required="true">	</mapping>
		<mapping type="String" excelTitle="bps范围" property="bps_range" required="false"></mapping>
		<mapping type="String" excelTitle="pps范围" property="pps_range" required="false">	</mapping>
		<mapping type="String" excelTitle="协议范围" property="prot" required="true"></mapping>
		<mapping type="String" excelTitle="源端口" property="src_port" required="false">	</mapping>
		<mapping type="String" excelTitle="目的端口" property="dest_port" required="false"></mapping>
		<mapping type="String" excelTitle="时间资源" property="time_res_id" required="true">	</mapping>
		<mapping type="String" excelTitle="是否启用" property="is_on" required="true"></mapping>
		<mapping type="String" excelTitle="是否为黑名单" property="rosterFlag" required="true">	</mapping>
		<mapping type="String" excelTitle="源ip范围" property="srcIpRange" required="false"></mapping>
		<mapping type="String" excelTitle="目的ip范围" property="dstIpRange" required="false"></mapping>
		
	</improtExcel>
</root>

第三步:读取xml

package test.util.excelutil;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.venustech.tsoc.cupid.Cupid;
import com.venustech.tsoc.cupid.nba.util.excelImportUtil.ExcelMappingBean;
import com.venustech.tsoc.cupid.nba.util.excelImportUtil.StringUtil;


/**
 * 规则导入  初始化映射 工具类
 * @author Administrator
 *
 */
public class RuleImportMappingUtil {

	private static RuleImportMappingUtil singleton = null;
	private Map<String, MappingBean> mappingBeanMap = new HashMap<String, MappingBean>();
	private Map<String, List<String>> titlesMap = new HashMap<String, List<String>>();
	
	public static RuleImportMappingUtil getInstance(){
		if(singleton == null){
			singleton = getSyncInstance();
		}
		return singleton;
	}
	/**
	 * 线程安全 获取实例
	 * @return
	 */
	private static synchronized RuleImportMappingUtil getSyncInstance(){
		return new RuleImportMappingUtil();
	}
	
	private RuleImportMappingUtil(){
		
		initExcelInfo();
	};
	/**
	 * 初始化 参数 读取 xml 文件,初始化 信息
	 */
	private void initExcelInfo(){

		String webinfo = Cupid.getContextValueAs(String.class, "conf.dir");
		String fileName = webinfo + "/nta/util/importExcelMappingRule.xml";
		SAXReader reader = new SAXReader();
		Document document;
		try {
			document = reader.read(new File(fileName));
			Element root = document.getRootElement();
			List<Element> excelEles = root.elements();
			for(Element excelEle : excelEles){
				String id = excelEle.attributeValue("id");
				String alias = excelEle.attributeValue("name");
				
				List<Element> mappingEles = excelEle.elements();
				List<String> titles = new ArrayList<String>();
				for(Element mappingEle : mappingEles){
					
					MappingBean excelMapping = new MappingBean();
					excelMapping.setExcelTitle(mappingEle.attributeValue("excelTitle"));
					excelMapping.setProperty(mappingEle.attributeValue("property"));
					excelMapping.setRequired(Boolean.valueOf(mappingEle.attributeValue("required")));
					if(excelMapping.isRequired()){
						titles.add(excelMapping.getExcelTitle());
					}
					
					excelMapping.setType(mappingEle.attributeValue("type"));
					
					
					if("map".equals(excelMapping.getType())){
						excelMapping.setMapName(mappingEle.elementText("mapName"));
						excelMapping.setMapDefaultValue(mappingEle.elementText("mapDefaultValue"));
						//setDictionaryMapMap(excelMapping.getMapName());
					}else if("db".equals(excelMapping.getType())){
						excelMapping.setClassName(mappingEle.elementText("className"));
						excelMapping.setAnnoName(mappingEle.elementText("annoName"));
						excelMapping.setMethodName(mappingEle.elementText("methodName"));
					}
					if(StringUtil.strIsNull(alias)){
						mappingBeanMap.put(id + "_" + excelMapping.getExcelTitle(), excelMapping);
					}else{
						mappingBeanMap.put(id + "$" + alias + "_" + excelMapping.getExcelTitle(), excelMapping);
					}
				}
				
				if(!titles.isEmpty()){
					if(StringUtil.strIsNull(alias)){
						titlesMap.put(id, titles);
					}else{
						titlesMap.put(id + "$" + alias + "_", titles);
					}
				}
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		} 
		
	
	}
	
	/**
	 * 
	 * @param packagePath  包路径
	 * @param name    总文件名  约定
	 * @param excelTitle  列的标题
	 * @return
	 */
	public MappingBean  getMappingBean(String packagePath , String name , String excelTitle){
		if(name == null || name.equals("")){
			
			return mappingBeanMap.get(packagePath + "_" + excelTitle);
		}else{
			return mappingBeanMap.get(packagePath  + "$" + name + "_" + excelTitle);
		}
	}
	/**
	 * 获取 所有的 字段值-中文含义
	 * @param packagePath
	 * @param name
	 * @return
	 */
	public List<String>  getAllTitles(String packagePath , String name ){
		if(name == null || name.equals("")){
					
					return titlesMap.get(packagePath);
				}else{
					return titlesMap.get(packagePath + "$" + name);
				}
				
		}
	
}


当然 这种方式 是遍历读取,你也可以指定 节点 读取:

1.获取文档的根节点.  
      Element root = document.getRootElement();  
    2.取得某个节点的子节点.  
      Element element=node.element(“四大名著");  
    3.取得节点的文字  
        String text=node.getText();  
    4.取得某节点下所有名为“csdn”的子节点,并进行遍历.  
       List nodes = rootElm.elements("csdn");   
         for (Iterator it = nodes.iterator(); it.hasNext();) {     
      Element elm = (Element) it.next();    
    // do something  
 }  
     5.对某节点下的所有子节点进行遍历.      
      for(Iterator it=root.elementIterator();it.hasNext();){        
        Element element = (Element) it.next();        
       // do something   
 }  
    6.在某节点下添加子节点  
      Element elm = newElm.addElement("朝代");  
    7.设置节点文字.  elm.setText("明朝");  
    8.删除某节点.//childElement是待删除的节点,parentElement是其父节点  parentElement.remove(childElment);  
    9.添加一个CDATA节点.Element contentElm = infoElm.addElement("content");contentElm.addCDATA(“cdata区域”);  

未完待续。。。。。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值