Java XML, Json - swkim0128/PARA GitHub Wiki
๊ณต๊ณต๊ธฐ๊ฐ์ด ๋ง๋ค์ด๋ด๋ ๋ชจ๋ ๊ณต์ ์ธ ์ ๋ณด
๊ฐ ๊ณต๊ณต๊ธฐ๊ด์ด ๋ณด์ ํ ๋ฐ์ดํฐ๋ฅผ ๊ฐ๋ฐฉํ์ฌ ๋๊ตฌ๋ ์ํ๋ ๊ธฐ๋ฅ์ ํ์ฉ ๊ฐ๋ฅ
www.data.go.kr ๋ฑ ํ์ ๊ฐ์ ํ ๊ฐ๋ณ ํค๋ฅผ ๋ฐ๊ธ ๋ฐ์ ์ฌ์ฉ
Markup Language
ํ๊ทธ๋ฑ์ ์ด์ฉํ์ฌ ๋ฌธ์๋ ๋ฐ์ดํฐ์ ๊ตฌ์กฐ๋ฅผ ๋ช ๊ธฐํ๋ ์ธ์ด
HTML, SGML, ...
XML
Extensible Markup Language
HTML๊ณผ ๋ฌ๋ฆฌ ํ์์ ๋ฐ๋ผ์ ํ๊ทธ๋ฅผ ํ์ฅํด์ ์ฌ์ฉ ๊ฐ๋ฅ
์ ํํ ๋ฌธ๋ฒ์ ์ง์ผ์ผ ๋์ : Well formed
๊ธฐ๋ณธ ๋ฌธ๋ฒ
๋ฌธ์์ ์์์ ๋ก ํ๋ค.
๋ง๋์ root element๊ฐ ์กด์ฌํ๋ค. ๋๋จธ์ง ํ๊ทธ๋ค์ Tree ํํ๋ก ๊ตฌ์ฑ๋๋ค.
์์ํ๊ทธ์ ์ข ๋ฃ ํ๊ทธ๋ ์ผ์นํด์ผ ํ๋ค.
์์ ํ๊ทธ๋ key-value๊ตฌ์กฐ์ ์์ฑ์ ๊ฐ์ง ์ ์๋ค.
์์ฑ ๊ฐ์ "" ๋๋ ''๋ก ๋ฌถ์ด์ ํํํ๋ค.
๋์๋ฌธ์๋ฅผ ๊ตฌ๋ณํ๋ค.
vaild
xml ํ๊ทธ๋ ์์ ๋กญ๊ฒ ์์ฑํ๊ธฐ ๋๋ฌธ์ ์ต์ด ์์ฑ์์ ์๋๋๋ก ์์ฑ๋๋์ง ํ์ธํ ํ์(DTD ๋๋ Schema๋ฅผ ์ด์ฉํด์ ๋ฌธ์์ ๊ท์น ์์ฑ)
ํ์ฑ
๋ฌธ์์์ ํ์ํ ์ ๋ณด๋ฅผ ์ป๊ธฐ ์ํด ํ๊ทธ๋ฅผ ๊ตฌ๋ณํ๊ณ ๋ด์ฉ์ ์ถ์ถํ๋ ๊ณผ์
SAX parser
Simple API for XML parser
๋ฌธ์๋ฅผ ์ฝ์ผ๋ฉด์ ํ๊ทธ์ ์์, ์ข ๋ฃ ๋ฑ ์ด๋ฒคํธ ๊ธฐ๋ฐ์ผ๋ก ์ฒ๋ฆฌํ๋ ๋ฐฉ์
๋น ๋ฅด๊ณ ํ๋ฒ์ ์ฒ๋ฆฌํ๊ธฐ ๋๋ฌธ์ ๋ค์ํ ํ์์ด ์ด๋ ต๋ค.
DOM parser
Document Object Model
๋ฌธ์๋ฅผ ๋ค ์ฝ๊ณ ๋ ํ ๋ฌธ์ ๊ตฌ์กฐ ์ ์ฒด๋ฅผ ์๋ฃ๊ตฌ์กฐ์ ์ ์ฅํ์ฌ ํ์ํ๋ ๋ฐฉ์
๋ค์ํ ํ์์ด ๊ฐ๋ฅํ์ง๋ง ๋๋ฆฌ๊ณ ๋ฌด๊ฑฐ์ฐ๋ฉฐ ํฐ ๋ฌธ์๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ด๋ ต๋ค.
์ ๋ณด๋ฅผ ์ ์ฅํ๊ธฐ ์ํ ํด๋์ค ๊ตฌ์ฑ
public class BoxOffice {
private Integer rank;
private String movieNm;
private Date openDt;
private Integer audiAcc;
public Date toDate(String date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
return format.arse(date);
}
catch (ParseException e) {
e.printStackTrace();
return new Date();
}
}
}
Handler ์์ฑ
public class BoxOfficeSaxParser extends DefaultHandler {
private final Fild xml = new File(".../boxoffice.xml");
private List<BoxOffice> list = new ArrayList<>();
private BoxOffice current;
private String content;
public List<BoxOffice> getBoxOffice() {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
parser.parse(xml, this);
}
catch (IOException | SAXException | ParserConfigurationException e) {
e.printStackTrace();
}
return list;
}
@Override
public void startDocument() throws SAXException {
Syste.out.println("๋ฌธ์์์");
}
@Override
public void endDocument() throws SAXException {
System.out.println("๋ฌธ์ ์ข
๋ฃ");
}
@Override
public void startElement(String uri, String localname, String qName,
Attributes attirbutes) throws SAXException {
if (qName.equals("dailyBoxOffice")) {
current = new BoxOffice();
}
}
@Override
public void characters(char[]ch, int start, int length) throws SAXException {
this.content = new String(ch, start, length);
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equals("dailyBoxOffice")) {
list.add(current);
current = null;
}
else if (qName.equals("rank")) {
current.setRank(Integer.parseInt(content));
}
else if (qName.equals("movieNm")) {
current.setMovieNm(Integer.parseInt(content));
}
else if (qName.equals("openDt")) {
current.setOpenDt(current.toDate(this.content));
}
else if (qName.equals("audiAcc")) {
current.setAudiAcc(Integer.parseInt(content));
}
}
}
XML ํ์ฑ ๋ฐ ๊ฒฐ๊ณผ ํ์ธ
public class SaxParserTest {
public static void main(String[] args) {
BoxOfficeSaxParser hanlder = new BoxOfficeSaxParser();
List<BoxOffice> list = hanlder.getBoxOffice();
for (BoxOffice boxOffice : list) {
System.out.println(boxOffice);
}
}
}
DOM Tree
๋ฌธ์๋ฅผ ๊ตฌ์ฑํ๋ ๋ชจ๋ ์์๋ฅผ Node(ํ๊ทธ, ์์ฑ, ๊ฐ)๋ก ๊ตฌ์ฑ
ํ๊ทธ๋ค์ root ๋ ธ๋(์ฃผ์๋ก)์ ์์์ผ๋ก ๋ถ๋ชจ-์์์ ๊ด๊ณ ๊ตฌ์ฑ