• 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>
    

      

     

  • 相关阅读:
    Single Number 解答
    Minimum Size Subarray Sum 解答
    Valid Anagram 解答
    Count Primes 解答
    fullCalendar 日历显示每天数据调用方法实践
    jqGrid列的统计
    关于 asp.net Web Api 上传文件请求内容过大404错误记录
    My97DatePicker只显示月份
    js 宏微任务执行顺序
    git 等官网下载慢方法
  • 原文地址:https://www.cnblogs.com/mu-tou-man/p/5059960.html
Copyright © 2020-2023  润新知