IT/Java

java - url을 통해 가져온 xml을 DOM 파서로 NodeList 출력

노마드오브 2018. 11. 20. 23:23

Company.java

package com.example.xml;


public class Company {

private String companyCd;

private String companyNm;

private String filmoNames;

public String getCompanyCd() {

return companyCd;

}

public void setCompanyCd(String companyCd) {

this.companyCd = companyCd;

}

public String getCompanyNm() {

return companyNm;

}

public void setCompanyNm(String companyNm) {

this.companyNm = companyNm;

}

public String getFilmoNames() {

return filmoNames;

}

public void setFilmoNames(String filmoNames) {

this.filmoNames = filmoNames;

}

@Override

public String toString() {

return "Company [companyCd=" + companyCd + ", companyNm=" + companyNm + ", filmoNames=" + filmoNames + "]";

}

}




MovieParser.java

package com.example.xml;


import java.net.MalformedURLException;

import java.net.URL;

import java.net.URLConnection;

import java.util.ArrayList;

import java.util.List;


import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;


import org.w3c.dom.Document;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;


public class MovieParser {


// DOM 파서를 사용하여 리스트로 결과 리턴

public List<Company> domParserToList() {

List<Company> list = new ArrayList<>();

String str = "http://kobis.or.kr/kobisopenapi/webservice/rest/company/searchCompanyList.xml?key=430156241533f1d058c603178cc3ca0e";

//str += "&itemPerPage=2";

URLConnection con = null;

try {

URL url = new URL(str);

con = url.openConnection();

} catch (Exception e) {

e.printStackTrace();

}  

// DOM 파서를 위한 참조변수 준비

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = null;

Document document = null;

try {

builder = factory.newDocumentBuilder();

document = builder.parse(con.getInputStream());

} catch (Exception e) {

e.printStackTrace();

}

// company 태그 이름을 기준으로 NodeList 가져오기

NodeList companyNodeList = document.getElementsByTagName("company");

for (int i=0; i<companyNodeList.getLength(); i++) {

Company company = new Company();

Node companyNode = companyNodeList.item(i);

NodeList childNodeList = companyNode.getChildNodes();

for (int j=0; j<childNodeList.getLength(); j++) {

Node childNode = childNodeList.item(j);

if (childNode.getNodeName().equals("companyCd")) {

String companyCd = childNode.getFirstChild().getNodeValue();

company.setCompanyCd(companyCd);

} else if (childNode.getNodeName().equals("companyNm")) {

String companyNm = childNode.getFirstChild().getNodeValue();

company.setCompanyNm(companyNm);

} else if (childNode.getNodeName().equals("filmoNames")) {

if (childNode.getFirstChild() != null) {

String filmoNames = childNode.getFirstChild().getNodeValue();

company.setFilmoNames(filmoNames);

}

}

} // 안쪽 for문 company레코드의 열 개수만큼 반복

list.add(company);

}

return list;

} // domParserToList()

public static void main(String[] args) {

MovieParser movieParser = new MovieParser();

List<Company> list = movieParser.domParserToList();

for (Company company : list) {

//System.out.println(company);

System.out.print("영화사코드: " + company.getCompanyCd());

System.out.print("\t영화사이름: " + company.getCompanyNm());

String strFilms = company.getFilmoNames();

if (strFilms == null) {

System.out.println("\n");

continue;

}

String[] arrFilms = strFilms.split(",");

System.out.println("영화이름: ");

for (String film : arrFilms) {

System.out.println(film.trim());

}

}

}


}