zoukankan      html  css  js  c++  java
  • XML解析代码

    import java.io.File;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Map;
    
    import org.apache.commons.lang.StringUtils;
    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.JDOMException;
    import org.jdom.input.SAXBuilder;
    
    public class ModelParser
    {
        private Map<String, List<ModelEntity>> modelEntitiesMap = new HashMap<String, List<ModelEntity>>();
    
        public ModelParser(String filePath, String fileName)
        {
    	parseFile(filePath, fileName);
        }
    
        private void parseFile(String filePath, String fileName)
        {
    	String fullPath = filePath + fileName;
    	File modelFile = new File(fullPath);
    
    	SAXBuilder saxBuilder = new SAXBuilder();
    	Document document = new Document();
    	try
    	{
    	    document = saxBuilder.build(modelFile);
    	}
    	catch (JDOMException e)
    	{
    	    // TODO Auto-generated catch block
    	    e.printStackTrace();
    	    return;
    	}
    	catch (IOException e)
    	{
    	    // TODO Auto-generated catch block
    	    e.printStackTrace();
    	    return;
    	}
    	Element rootElement = document.getRootElement();
    	List<Element> firstElementList = rootElement.getChildren();
    	for (Element element : firstElementList)
    	{
    	    String objName = element.getName();
    	    List<ModelEntity> modelEntitiesList = getModelEntities(element);
    	    modelEntitiesMap.put(objName, modelEntitiesList);
    	}
        }
    
        private List<ModelEntity> getModelEntities(Element firstElement)
        {
    
    	List<ModelEntity> modelEntitiesList = new LinkedList<ModelEntity>();
    	List<Element> tableElementList = firstElement.getChildren("table");
    	for (Element secondElement : tableElementList)
    	{
    	    List<Element> columnElementList = secondElement.getChildren();
    	    List<String> columnList = new LinkedList<String>();
    	    for (Element thirdElement : columnElementList)
    	    {
    		if (StringUtils.isNotBlank(thirdElement.getTextTrim()))
    		    columnList.add(thirdElement.getTextTrim());
    	    }
    	    ModelEntity modelEntity = new ModelEntity();
    	    String name = secondElement.getAttributeValue("name");
    	    String key = secondElement.getAttributeValue("key");
    	    modelEntity.setKey(key);
    	    modelEntity.setColumList(columnList);
    	    modelEntity.setName(name);
    	    modelEntitiesList.add(modelEntity);
    	}
    	return modelEntitiesList;
    
        }
    
        public Map<String, List<ModelEntity>> getModelEntitiesMap()
        {
    	return modelEntitiesMap;
        }
    
    }
    

    xml文件如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <model>
    	<cell>
    		<table name="csv1" key="cId">
    			<column>cId</column>
    			<column>zhang</column>
    			<column>xi</column>
    		</table>
    		<table name="csv2" key="pId">
    			<column>pId</column>
    			<column>zhang    </column>
    			<column>wen   </column>
    			<column></column>
    		</table>
    	</cell>
    	
    	<ercl>
    		<table name="csv3" key="eId">
    			<column>eId</column>
    			<column>jing</column>
    			<column>tao</column>
    		</table>
    		<table name="csv4" key="dId">
    			<column>dId</column>
    			<column>yang</column>
    			<column>cun</column>
    		</table>
    	</ercl>
    </model>
    

      

     

  • 相关阅读:
    物理层和数据链路层
    Python :Dive Into Python 中文版 读后感_第五章__类与对象
    关于路由器的2种接法
    关于设计模式的一些理解和感悟
    C# 字符串加密
    Sql server 查询锁表和接锁表
    关于中文参数编码的一些文章推荐
    写与悟
    http 返回的状态码以及含义
    js 操作<input type="hidden" runnat="server" id="month">服务器控件
  • 原文地址:https://www.cnblogs.com/mu-tou-man/p/5059960.html
Copyright © 2011-2022 走看看