업무를 진행하다가 오랜만에 XML을 파싱할 일이 생겨서 예제를 만들어본다.
우선 샘플로 사용할 sample.xml이다.
<?xml version="1.0" encoding="utf-8" ?>
<in>
<propertie name="cnt_1"></propertie>
<record name="record_1" ref="cnt_1">
<propertie length="1"></propertie>
<propertie length="2"></propertie>
</record>
<propertie name="cnt_2"></propertie>
<record name="record_2" ref="cnt_2">
<propertie length="1"></propertie>
<propertie length="2"></propertie>
<propertie length="3"></propertie>
<propertie length="4"></propertie>
<propertie length="5"></propertie>
<propertie length="6"></propertie>
</record>
</in>
다음으로는 JAVA소스다.
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XmlParsingTestMain {
public static void main(String argv[]) {
try {
File file = new File("sample.xml");
DocumentBuilderFactory docBuildFact = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuild = docBuildFact.newDocumentBuilder();
Document doc = docBuild.parse(file);
doc.getDocumentElement().normalize();
System.out.println("Root element : " + doc.getDocumentElement().getNodeName());
System.out.println();
NodeList nodeList = doc.getElementsByTagName("in");
List<Object> inList = new ArrayList<Object>();
System.out.println("nodeList.getLength() ::: " + nodeList.getLength());
nodeList = nodeList.item(0).getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
System.out.println("---------- nodeList "+ i + "번째 ------------------");
if (node.getNodeType() == Node.ELEMENT_NODE) {
// node엘리먼트
Element element = (Element)node;
// node 명
System.out.println("node name : " + node.getNodeName());
Map<String, Object> map = new HashMap<String, Object>();
if(!element.getAttribute("name").equals("")) {
map.put("name", element.getAttribute("name"));
}
if(!element.getAttribute("ref").equals("")) {
map.put("ref", element.getAttribute("ref"));
}
inList.add(map);
if(node.getNodeName().equals("record")) {
NodeList dataList = node.getChildNodes();
List<Map<String, Object>> inList2 = new ArrayList<Map<String, Object>>();
System.out.println("dataList.getLength() ::: " + dataList.getLength());
for (int j = 0; j < dataList.getLength(); j++) {
Node node2 = dataList.item(j);
if (node2.getNodeType() == Node.ELEMENT_NODE) {
Element ele2 = (Element) node2;
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("ref", ele2.getAttribute("ref"));
map2.put("length", ele2.getAttribute("length"));
inList2.add(map2);
}
}
map.put("record", inList2);
}
}
System.out.println("---------------------------------------------");
}
System.out.println(inList);
for(Object node : inList) {
if(node instanceof Map) {
System.out.println(node);
} else if (node instanceof List) {
List<Map<String, Object>> list = (ArrayList<Map<String, Object>>)node;
for(Object map : list) {
System.out.println(map);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
'자바·JSP' 카테고리의 다른 글
리소스 파일 읽기 (0) | 2020.04.20 |
---|---|
자바 클래스에서 리소스 로드하기 (0) | 2020.04.18 |
[JAVA] OS별로 CPU정보 가져오기 (0) | 2019.04.05 |
Apache Configuration for Java WebStart (0) | 2018.12.14 |
POST, GET 방식 확인 (0) | 2018.01.25 |