아래에 작성된 예제는 아래의 xml에서 dataset의 id값을 xml에서 찾아서 그 결과를 반환하여 GauceDataSet객체를
생성하여 반환하는 샘플 코드.
1. 샘플에서 사용할 XML은 아래와 같다.
<?xml version="1.0" encoding="UTF-8"?>
<root>
<!-- 데이터셋 샘플 1 -->
<dataset id="oSample">
<column type="string" name="TEST1" length="20" decimal="0" constraint="normal"/>
<column type="string" name="TEST2" length="20" decimal="0" constraint="normal"/>
<column type="string" name="TEST3" length="20" decimal="0" constraint="normal"/>
<column type="string" name="TEST4" length="20" decimal="0" constraint="normal"/>
<column type="string" name="TEST5" length="20" decimal="0" constraint="normal"/>
</dataset>
<!-- 데이터셋 샘플 2 -->
<dataset id="oSample2">
<column type="string" name="NAME1" length="20" decimal="0" constraint="normal"/>
<column type="string" name="NAME2" length="20" decimal="0" constraint="normal"/>
<column type="string" name="NAME3" length="20" decimal="0" constraint="normal"/>
<column type="string" name="NAME4" length="20" decimal="0" constraint="normal"/>
</dataset>
</root>
2. 소스는 아래와 같이 작성하였음...;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;
import com.gauce.GauceDataColumn;
import com.gauce.GauceDataSet;
public class XmlUtils {
/**
* XML에서 데이터헤더를 정의하여 데이터셋을 반환한다.
* @param p_xmlDataSetId XML에 기술된 데이터셋의 ID
* @param p_dataSet GauceDataSet Instance
*/
public static GauceDataSet getGauceDataSet(String p_xmlDataSetId, GauceDataSet p_dataSet) {
StringBuffer xpath = new StringBuffer();
xpath.append("/root/dataset[@id='").append(p_xmlDataSetId).append("'"); // XPATH 정의
if (p_dataSet == null) {
p_dataSet = new GauceDataSet(p_xmlDataSetId);
}
SAXBuilder builder = new SAXBuilder();
Document doc = null;
try {
doc = builder.build(new File("c:\\dataset.xml"));
// XPATH를 사용하여 node select
Element e = (Element)XPath.selectSingleNode(doc, xpath.toString());
List columnList = e.getChildren();
for (int j=0; j<columnList.size(); j++) {
Element el = (Element)columnList.get(j);
String columnName = el.getAttributeValue("name");
String tmpType = el.getAttributeValue("type");
int columnLength = Integer.parseInt(el.getAttributeValue("length"));
int columnDecimal = Integer.parseInt(el.getAttributeValue("decimal"));
String tmpConstraint = el.getAttributeValue("constraint");
/** 컬럼 타입을 상수값으로 변환 **/
int columnType = -1;
if (tmpType.equals("int")) {
columnType = GauceDataColumn.TB_INT;
} else if (tmpType.equals("decimal")) {
columnType = GauceDataColumn.TB_DECIMAL;
} else if (tmpType.equals("url")) {
columnType = GauceDataColumn.TB_URL;
} else if (tmpType.equals("bigint")) {
columnType = GauceDataColumn.TB_BIGINT;
} else if (tmpType.equals("blob")) {
columnType = GauceDataColumn.TB_BLOB;
} else {
columnType = GauceDataColumn.TB_STRING; // default
}
/** Constraint를 상수값으로 변환 **/
int columnConstraint = -1;
if (tmpConstraint.equals("notnull")) {
columnConstraint = GauceDataColumn.TB_NOTNULL;
} else if (tmpConstraint.equals("key")) {
columnConstraint = GauceDataColumn.TB_KEY;
} else {
columnConstraint = GauceDataColumn.TB_NORMAL;
}
p_dataSet.addDataColumn(new GauceDataColumn(columnName, columnType, columnLength, columnDecimal, columnConstraint ));
}
return p_dataSet;
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (JDOMException jdome) {
jdome.printStackTrace();
}
return null;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
GauceDataSet ds = getGauceDataSet("oSample", null);
GauceDataSet ds2 = getGauceDataSet("oSample2", null);
System.out.println("컬럼 갯수1 : " + ds.getDataColCnt());
System.out.println("컬럼 갯수2 : " + ds2.getDataColCnt());
}
}
No comments:
Post a Comment