하고재비
[JAVA] XML Parse / Data Access Object Example 본문
-------------------------------------------
ExamMain.java
public class ExamMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
XmlDataService xmlImpl = new XmlDataService();
ArrayList xmlList = xmlImpl.Read("src/Example/Example.xml");
new ArrayListHandler().showArrayList(xmlList);
}
}
-------------------------------------------
<Employees> <Employee Id="1001"> <Name>Tim</Name> <Dept>Sales</Dept> </Employee> <Employee Id="1002"> <Name>John</Name> <Dept>HR</Dept> </Employee> </Employees> |
-------------------------------------------
import java.util.ArrayList;
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 XmlDataService {
private static String getTagValue(String tag, Element eElement) {
NodeList nlList = eElement.getElementsByTagName(tag).item(0).getChildNodes();
Node nValue = (Node) nlList.item(0);
if (nValue == null)
return null;
return nValue.getNodeValue();
}
public ArrayList Read(String url) {
ArrayList xmlList = new ArrayList();
int count = 0;
try
{
// parsing할 url 지정(API 키 포함해서)
// 1. 문서 읽기위한 공장 생성
DocumentBuilderFactory dbFactoty = DocumentBuilderFactory.newInstance();
// 2. 빌더 생성
DocumentBuilder dBuilder = dbFactoty.newDocumentBuilder();
// 3. 빌더를 통해 xml 을 document객체에 대입
Document doc = dBuilder.parse(url);
// root tag
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
// 파싱할 tag
Element root = doc.getDocumentElement();
NodeList nList = root.getElementsByTagName("Employee");
System.out.println("파싱할 리스트 수 : "+ nList.getLength());
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
String name = getTagValue("Name", eElement);
String dept = getTagValue("Dept", eElement);
//System.out.println("Name : " + getTagValue("Name", eElement));
//System.out.println("Dept : " + getTagValue("Dept", eElement));
XmlDataAccess xmldata = new XmlDataAccess(name, dept);
xmlList.add(xmldata);
count++;
} // if end
} // for end
return xmlList;
}
catch (Exception e)
{
e.printStackTrace();
return null;
} // try~catch end
}
}
-------------------------------------------
public class XmlDataAccess
{
private String name;
private String dept;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public XmlDataAccess() { }
public XmlDataAccess(String name, String dept)
{
this.setName(name);
this.setDept(dept);
}
public void clear()
{
this.name = "";
this.dept = "";
}
}
-------------------------------------------
import java.util.ArrayList;
public class ArrayListHandler {
public void showArrayList (ArrayList xmlList)
{
for(XmlDataAccess xmlData :xmlList)
{
System.out.println(xmlData.getName()+ " / " + xmlData.getDept());
}
}
}
'JAVA' 카테고리의 다른 글
[JAVA] getter, setter (0) | 2018.07.20 |
---|---|
[JAVA] xml생성 (0) | 2018.04.19 |
[JAVA]TCP/IP (0) | 2018.01.29 |
[자료구조] 연결리스트 (0) | 2017.12.18 |
[JAVA]ArrayList (0) | 2017.12.13 |