CompanyHandler.java
package com.example.xml;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class CompanyHandler extends DefaultHandler {
private List<Company> list = new ArrayList<>();
private Company company;
private StringBuilder sb = new StringBuilder();
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equals("company")) {
company = new Company();
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equals("companyCd")) {
company.setCompanyCd(sb.toString().trim());
sb.setLength(0);
} else if (qName.equals("companyNm")) {
company.setCompanyNm(sb.toString().trim());
sb.setLength(0);
} else if (qName.equals("filmoName")) {
company.setFilmoNames(sb.toString().trim());
sb.setLength(0);
} else if (qName.endsWith("company")) {
list.add(company);
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
sb.append(ch, start, length);
}
public List<Company> getList() {
return list;
}
public void setList(List<Company> list) {
this.list = list;
}
}
MovieSAXParser.java
package com.example.xml;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
public class MovieSAXParser {
public List<Company> saxParserToList() {
String str = "http://kobis.or.kr/kobisopenapi/webservice/rest/company/searchCompanyList.xml?key=430156241533f1d058c603178cc3ca0e";
str += "&itemPerPage=5";
URLConnection con = null;
try {
URL url = new URL(str);
con = url.openConnection();
} catch (Exception e) {
e.printStackTrace();
}
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = null;
CompanyHandler handler = new CompanyHandler();
// 핸들러
try {
parser = factory.newSAXParser();
parser.parse(con.getInputStream(), handler);
} catch (Exception e) {
e.printStackTrace();
}
List<Company> list = handler.getList();
return list;
}
public static void main(String[] args) {
MovieSAXParser movieSAXParser = new MovieSAXParser();
List<Company> list = movieSAXParser.saxParserToList();
for (Company company : list) {
System.out.println(company.toString());
}
}
}
MovieJSONParser.java
package com.example.xml;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class MovieJSONParser {
public static void main(String[] args) {
String str = "http://kobis.or.kr/kobisopenapi/webservice/rest/company/searchCompanyList.json?key=430156241533f1d058c603178cc3ca0e";
URLConnection con = null;
BufferedReader reader = null;
JSONObject jsonObject = null;
try {
URL url = new URL(str);
con = url.openConnection();
InputStream is = con.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, "utf-8"));
// JSON 파서 준비
JSONParser parser = new JSONParser();
// JSONArray 또는 JSONObject 둘 중에 하나가 리턴됨
jsonObject = (JSONObject) parser.parse(reader); // 파싱 수행
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(jsonObject);
JSONObject objCompanyListResult = (JSONObject) jsonObject.get("companyListResult");
JSONArray arrCompanyList = (JSONArray) objCompanyListResult.get("companyList");
System.out.println(arrCompanyList);
List<Company> list = new ArrayList<>();
for (int i=0; i<arrCompanyList.size(); i++) {
Company company = new Company();
JSONObject objCompany = (JSONObject) arrCompanyList.get(i);
String companyCd = (String) objCompany.get("companyCd");
String companyNm = (String) objCompany.get("companyNm");
String filmoNames = (String) objCompany.get("filmonNames");
company.setCompanyCd(companyCd);
company.setCompanyNm(companyNm);
company.setFilmoNames(filmoNames);
list.add(company);
}
for (Company company : list) {
System.out.println(company);
}
} // main
}
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 + "]";
}
}