XML 확인 코드

4112 단어
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>

  
 
 

좋은 웹페이지 즐겨찾기