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>
    

      

     

  • 相关阅读:
    Java实现 蓝桥杯VIP 算法提高 师座操作系统
    Java实现 蓝桥杯VIP 算法提高 师座操作系统
    Java实现 蓝桥杯VIP 算法提高 师座操作系统
    Qt_5_3_MSVC2012-编译QFtp-qt5编译QFtp
    存储入门 – RAID技术(大图解释)
    为什么要关闭360云盘:新来的美工嫌我们logo太丑,所以就决定关闭了。这个理由怎么样
    360云盘服务端前来回答
    SMB2 Protocol – 简介(应用层协议主要用于在计算机间共享文件、打印机、串口等)
    MVC 5
    集合
  • 原文地址:https://www.cnblogs.com/mu-tou-man/p/5059960.html
Copyright © 2011-2022 走看看