๋ชฉ์ฐจ
[XML App05]
/*========================
XMLDOM.java
========================*/
package com.test;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
public class XMLDOM
{
public static String getText(Element parent, String tagName)
{
String result = "";
// ๋์ ํ๊ทธ(tagName) ๊ฐ์ฒด์ ์ฒซ ๋ฒ์งธ ์์ ๋
ธ๋ ์ป์ด์ค๊ธฐ
Node node = parent.getElementsByTagName(tagName).item(0);
Element element = (Element)node;
// ๋์ ์๋ฆฌ๋จผํธ(element)์ ์์ ๋
ธ๋(ํ
์คํธ๋
ธ๋)์ ๊ฐ ์ป์ด์ค๊ธฐ
result = element.getChildNodes().item(0).getNodeValue();
return result;
}
}
XmlDomTest06.java
/*================================================
XmlDomTest06.java
- ์ฝ์ ๊ธฐ๋ฐ ์๋ฐ ํ๋ก๊ทธ๋จ
- XML DOM ํ์ฉ → ๋ก์ปฌ(local) XML ์ฝ์ด๋ด๊ธฐ
(rss.xml)
โป ๊ธฐ์์ฒญ ๋ ์จ๋๋ฆฌ๋ก๋ถํฐ ์ป์ด๋ธ ๋ฐ์ดํฐ
=================================================*/
package com.test;
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 XmlDomTest06
{
public static void main(String[] args)
{
try
{
// 1. XML ํ์ผ ๋ฉ๋ชจ๋ฆฌ์ ๋ก๋
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
String url = "rss.xml";
Document xmlObj = builder.parse(url);
// ๋ฃจํธ ์๋ฆฌ๋จผํธ ์ ๊ทผ
Element root = xmlObj.getDocumentElement();
// 3-1. ํ์ดํ ์ถ์ถํ๊ธฐ
Node itemNode = root.getElementsByTagName("item").item(0);
Element itemElement = (Element)itemNode;
System.out.printf("%s%n%n", XMLDOM.getText(itemElement, "title"));
//--==>> ์ ๊ตญ ์ก์ ์ค๊ธฐ์๋ณด - 2024๋
02์ 02์ผ (๊ธ)์์ผ 06:00 ๋ฐํ
// 3-2. ๊ธฐ์์ ๋ง ์ถ์ถํ๊ธฐ
Node wfNode = root.getElementsByTagName("wf").item(0);
Element wfElement = (Element)wfNode;
System.out.println("[๊ธฐ์์ ๋ง] ---------------------------------------------------");
System.out.printf("%s%n%n", wfElement.getTextContent().replaceAll("<br />", "\n"));
// 3-3. ๋์๋ณ ๊ธฐ์ ์๋ณด ์ถ์ถํ๊ธฐ
System.out.println("[์ก์๋ ์จ] ---------------------------------------------------");
NodeList locationNodeList = root.getElementsByTagName("location");
for (int i = 0; i < locationNodeList.getLength(); i++)
{
Node locationNode = locationNodeList.item(i);
Element locationElement = (Element)locationNode;
System.out.printf("๋์ : %s%n", XMLDOM.getText(locationElement, "city"));
System.out.println("---------------------------------------------------");
NodeList dataNodeList = locationElement.getElementsByTagName("data");
for (int j = 0; j < dataNodeList.getLength(); j++)
{
Node dataNode = dataNodeList.item(j);
Element dataElement = (Element)dataNode;
System.out.printf("๋ ์ง์๊ฐ : %s%n๋ ์จ์๋ณด : %s%n์ต์ ์จ๋ : %sโ%n์ต๊ณ ์จ๋ : %sโ%n๊ฐ์ํ๋ฅ : %s%%%n"
, XMLDOM.getText(dataElement, "tmEf")
, XMLDOM.getText(dataElement, "wf"), XMLDOM.getText(dataElement, "tmn")
, XMLDOM.getText(dataElement, "tmx"), XMLDOM.getText(dataElement, "rnSt"));
System.out.println("---------------------------------------------------");
}
System.out.println("====================================================");
}
} catch (Exception e)
{
System.out.println(e.toString());
}
}
}
[ XmlApp 06 ]
XMLDOM.java
/*========================
XMLDOM.java
========================*/
package com.test;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
public class XMLDOM
{
public static String getText(Element parent, String tagName)
{
String result = "";
// ๋์ ํ๊ทธ(tagName) ๊ฐ์ฒด์ ์ฒซ ๋ฒ์งธ ์์ ๋
ธ๋ ์ป์ด์ค๊ธฐ
Node node = parent.getElementsByTagName(tagName).item(0);
Element element = (Element)node;
// ๋์ ์๋ฆฌ๋จผํธ(element)์ ์์ ๋
ธ๋(ํ
์คํธ๋
ธ๋)์ ๊ฐ ์ป์ด์ค๊ธฐ
result = element.getChildNodes().item(0).getNodeValue();
return result;
}
}
XmlDomTest07.java
/*==================================================
XmlDomTest07.java
- ์ฝ์ ๊ธฐ๋ฐ ์๋ฐ ํ๋ก๊ทธ๋จ
- XML DOM ํ์ฉ → ์๊ฒฉ(remote) XML ์ฝ์ด๋ด๊ธฐ
(http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=108)
โป ๊ธฐ์์ฒญ ๋ ์จ๋๋ฆฌ๋ก๋ถํฐ ์ป์ด๋ธ ๋ฐ์ดํฐ
==================================================*/
/*
http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=108 : ์ ๊ตญ
http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=109 : ์์ธ. ๊ฒฝ๊ธฐ๋
http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=105 : ๊ฐ์๋
http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=131 : ์ถฉ๋ถ
http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=133 : ์ถฉ๋จ
http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=146 : ์ ๋ถ
http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=156 : ์ ๋จ
http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=143 : ๊ฒฝ๋ถ
http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=159 : ๊ฒฝ๋จ
http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=184 : ์ ์ฃผํน๋ณ์์น๋
*/
package com.test;
import java.net.URL;
import java.util.Scanner;
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;
import org.xml.sax.InputSource;
public class XmlDomTest07
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String[] stnId = {"108", "109", "105", "131", "133", "146", "156", "143", "159", "184"};
do
{
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document xmlObj = null;
// Local XML ํ์ผ์ธ ๊ฒฝ์ฐ...
/*
String url = "rss.xml";
xmlObj = builder.parse(url);
*/
// Remote XML ํ์ผ์ธ ๊ฒฝ์ฐ...
// ์๋ด → ์ธ๋ถ์ ์
๋ ฅ๊ฐ ์ฒ๋ฆฌ
System.out.println("โ โ โ ๊ธฐ์์ฒญ ์ก์ ์ค๊ธฐ ์๋ณด โ โ โ ");
System.out.println("-----------------------------------");
System.out.println("1. ์ ๊ตญ ");
System.out.println("2. ์์ธ. ๊ฒฝ๊ธฐ๋");
System.out.println("3. ๊ฐ์๋ ");
System.out.println("4. ์ถฉ๋ถ ");
System.out.println("5. ์ถฉ๋จ ");
System.out.println("6. ์ ๋ถ ");
System.out.println("7. ์ ๋จ ");
System.out.println("8. ๊ฒฝ๋ถ ");
System.out.println("9. ๊ฒฝ๋จ ");
System.out.println("10. ์ ์ฃผํน๋ณ์์น๋");
System.out.println("-----------------------------------");
System.out.print(">>์ง์ญ ์ ํ(์ข
๋ฃ 0) : ");
String m = sc.next();
if (m.equals("0"))
break;
//System.out.println("๊ณ์ ์งํ");
String str = String.format("http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=%s"
, stnId[Integer.parseInt(m)-1]);
// ํ์ธ3
//System.out.println("์์ฒญ ์ฃผ์ : " + str);
//--==>> ใ3. ๊ฐ์๋ใ ์ ํ ์
//-->> ์์ฒญ ์ฃผ์ : http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=105
//--==>> ใ7. ์ ๋จใ ์ ํ ์
//--==>> ์์ฒญ ์ฃผ์ : http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=156
// ๋ฌธ์์ด๋ก ๊ตฌ์ฑํ ์์ฒญ ์ฃผ์๋ฅผ URL ๊ฐ์ฒด ๊ตฌ์ฑ → ์ ๋ ๊ฒฝ๋ก ์ง์ ๋ฐฉ์
URL url = new URL(str);
// ๊ตฌ์ฑํ URL ๋ก ์ ๊ทผํ์ฌ ๋ฐ์ดํฐ ๋ด์ฉ ์ฝ์ด์ค๊ธฐ → ์คํธ๋ฆผ(Stream)
InputSource is = new InputSource(url.openStream());
xmlObj = builder.parse(is);
//----------------------------------------------------------------
// ๋ฃจํธ ์๋ฆฌ๋จผํธ ์ ๊ทผ
Element root = xmlObj.getDocumentElement();
// ํ์ดํ ์ถ์ถ
Node itemNode = root.getElementsByTagName("item").item(0);
Element itemElement = (Element)itemNode;
System.out.printf("%s%n", XMLDOM.getText(itemElement, "title"));
// ๊ธฐ์์ ๋ง ์ถ์ถํ๊ธฐ
System.out.println("[๊ธฐ์ ์ ๋ง] -----------------------------------------");
Node wfNode = root.getElementsByTagName("wf").item(0);
Element wfElement = (Element)wfNode;
System.out.printf("%s%n%n", wfElement.getTextContent().replaceAll("<br />", "\n"));
//๋์๋ณ ๊ธฐ์์๋ณด ์ถ์ถํ๊ธฐ
System.out.println("[์ก์ ๋ ์จ] -----------------------------------------");
NodeList locationNodeList = root.getElementsByTagName("location");
for (int i = 0; i < locationNodeList.getLength(); i++)
{
// ๋์..
Node locationNode = locationNodeList.item(i);
Element locationElement = (Element)locationNode;
System.out.printf("๋์ : %s%n", XMLDOM.getText(locationElement, "city"));
System.out.println("----------------------------------------------------------------------------");
NodeList dataNodeList = locationElement.getElementsByTagName("data");
// ๋ ์ง ์๊ฐ / ๋ ์จ ์๋ณด / ์ต์ ์จ๋ ~ ์ต๊ณ ์จ๋ / ๊ฐ์ ํ๋ฅ
for (int j = 0; j < dataNodeList.getLength(); j++)
{
Node dataNode = dataNodeList.item(j);
Element dataElement = (Element)dataNode;
System.out.printf("%s / %s / %sโ ~ %sโ / %s%%%n"
, XMLDOM.getText(dataElement, "tmEf"), XMLDOM.getText(dataElement, "wf")
, XMLDOM.getText(dataElement, "tmn"), XMLDOM.getText(dataElement, "tmx")
, XMLDOM.getText(dataElement, "rnSt"));
}
System.out.println("----------------------------------------------------------------------------");
}
} catch (Exception e)
{
System.out.println(e.toString());
}
} while (true);
}
}
[XmlApp 07]
โ ์ค์ต xml url
๋๋ณด๊ธฐ
https://fs.jtbc.co.kr/RSS/newsflash.xml
XMLDOM.java
/*========================
XMLDOM.java
========================*/
package com.test;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
public class XMLDOM
{
public static String getText(Element parent, String tagName)
{
String result = "";
// ๋์ ํ๊ทธ(tagName) ๊ฐ์ฒด์ ์ฒซ ๋ฒ์งธ ์์ ๋
ธ๋ ์ป์ด์ค๊ธฐ
Node node = parent.getElementsByTagName(tagName).item(0);
Element element = (Element)node;
// ๋์ ์๋ฆฌ๋จผํธ(element)์ ์์ ๋
ธ๋(ํ
์คํธ๋
ธ๋)์ ๊ฐ ์ป์ด์ค๊ธฐ
result = element.getChildNodes().item(0).getNodeValue();
return result;
}
}
XmlDomTest08.java
/*==================================================
XmlDomTest08.java
- ์ฝ์ ๊ธฐ๋ฐ ์๋ฐ ํ๋ก๊ทธ๋จ
- XML DOM ํ์ฉ → ์๊ฒฉ(remote) XML ์ฝ์ด๋ด๊ธฐ
(https://fs.jtbc.co.kr/RSS/newsflash.xml)
โป ์ธ๋ก ์ฌ ๋ด์ค๋ก๋ถํฐ ์ป์ด๋ธ ๋ฐ์ดํฐ
==================================================*/
/*
title> JTBC News
link> https://fs.jtbc.co.kr/RSS/newsflash.xml
description> ์๋ณด RSS
copyright> Copyright(C) JTBC All rights reserved.
์ฃผ์ ๊ธฐ์ฌ-------------------------------
title> [๋ ์จ] ์ ๊ตญ ๋์ฒด๋ก ํ๋ฆผ…๊ณณ๊ณณ ๋ฏธ์ธ๋จผ์ง '๋์จ'
description> ์ค๋(2์ผ)์ ์ ๊ตญ์ด ๋์ฒด๋ก ํ๋ฆฐ ๊ฐ์ด๋ฐ, ์ผ๋ถ ์ง์ญ์ ๋ฏธ์ธ๋จผ์ง๊ฐ '๋์จ' ์์ค์ ๋ณด์ด๊ฒ ์ต๋๋ค.๋ฎ ์ต๊ณ ๊ธฐ์จ์ 3๋์์ 10๋๋ก ํฌ๊ทผํ ๋ ์จ๊ฐ ์์๋ฉ๋๋ค.
link> https://news.jtbc.co.kr/article/article.aspx?news_id=NB12163616
pubDate> 2024.02.02
*/
package com.test;
import java.net.URL;
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;
import org.xml.sax.InputSource;
public class XmlDomTest08
{
public static void main(String[] args)
{
/*
1. ์๊ฒฉ XML ์ ๋ณด๋ฅผ ์์ฒญํ๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฉ๋ชจ๋ฆฌ์ ๋ก๋
→ XML DOM ๊ตฌ์ฑ
·DB ํ์ฉ
·DBF ํ์ฉ
·XML ๋ก๋ฉ
2. ๋ฃจํธ ์๋ฆฌ๋จผํธ ์ ๊ทผ
3. ํน์ ํ์ ์๋ฆฌ๋จผํธ ์ ๊ทผ
4. ํ
์คํธ ๋
ธ๋ ์ ๊ทผ
→ ํ์ํ ๋ฐ์ดํฐ ํ๋
5. ๊ฒฐ๊ณผ ์ฒ๋ฆฌ(์ถ๋ ฅ)
*/
try
{
// โป DOM(Document Object Model)
// - XML ์ด๋ HTML Document(๋ฌธ์)๋ฅผ ์์ฉ ํ๋ก๊ทธ๋จ์์ ์ฌ์ฉํ๊ธฐ ์ํ API ๊ท๊ฒฉ
// - DOM ์ Document(๋ฌธ์)์ ๊ฐ ๋ถ๋ถ๋ค์ ๊ฐ์ฒด(Object)๋ก ํํํ API
// โป DOM(Document Object Model) ํ์(Parser)
// - XML ๋ฌธ์๋ฅผ ์ฝ๊ณ , ํด์ํ ํ
// ํด์ํ ๊ฒฐ๊ณผ๋ฅผ ๋ฉ๋ชจ๋ฆฌ์ DOM ๊ฐ์ฒด ํธ๋ฆฌ ๊ตฌ์กฐ๋ก ์์ฑ์ํค๋ ํ์ (Parser)
// - ์ํ๋ ๋ฐ์ดํฐ์ ์ ๊ทผํ ์ ์๋๋ก ํด์ฃผ์ด
// ๋์ ๋ฐ์ดํฐ๋ฅผ ๊ฒ์, ์์ , ์ญ์ ํ ์ ์๋๋ก ์ง์
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document xmlObj = null;
String str = "https://fs.jtbc.co.kr/RSS/newsflash.xml";
URL url = new URL(str);
InputSource is = new InputSource(url.openStream());
xmlObj = builder.parse(is);
Element root = xmlObj.getDocumentElement();
Node node = root.getChildNodes().item(0);
Element element = (Element)node;
System.out.printf("title> %s%nlink> %s%ndescription> %s%ncopyright> %s%n"
, XMLDOM.getText(element, "title"), XMLDOM.getText(element, "link")
, XMLDOM.getText(element, "description"), XMLDOM.getText(element, "copyright"));
System.out.println();
// ์ฃผ์๊ธฐ์ฌ ๋ฐ์์ค๊ธฐ
NodeList itemList = root.getElementsByTagName("item");
System.out.println("์ฃผ์๊ธฐ์ฌ --------------------------------------");
for (int i = 0; i < itemList.getLength(); i++)
{
Node itemNode = itemList.item(i);
Element itemElement = (Element)itemNode;
System.out.printf("title> %s%ndescription> %s%nlink> %s%npuDate> %s%n"
, XMLDOM.getText(itemElement, "title"), XMLDOM.getText(itemElement, "description")
, XMLDOM.getText(itemElement, "link"), XMLDOM.getText(itemElement, "pubDate"));
System.out.println();
}
} catch (Exception e)
{
System.out.println(e.toString());
}
}
}
[XmlApp 08]
weatherDTO.java
/*=====================
WeatherDTO.java
=======================*/
package com.test;
public class WeatherDTO
{
// ์ฃผ์ ์์ฑ ๊ตฌ์ฑ
private String tmEf, wf, tmn, tmx, rnSt, img;
//-- tmEf → ๋ ์ง์ ์๊ฐ ํ๊ทธ
// 2024-02-25 00:00
//-- wf → ๋ ์จ ์๋ณด ํ๊ทธ
// โ ๋ง์ โ ๊ตฌ๋ฆ์กฐ๊ธ
// โ ๊ตฌ๋ฆ๋ง์, ๊ตฌ๋ฆ๋ง๊ณ ๋น, ๊ตฌ๋ฆ๋ง๊ณ ๋น/๋, ๊ตฌ๋ฆ๋ง๊ณ ๋/๋น, ๊ตฌ๋ฆ๋ง๊ณ ๋
// โ ํ๋ฆผ, ํ๋ฆฌ๊ณ ๋น, ํ๋ฆฌ๊ณ ๋น/๋, ํ๋ฆฌ๊ณ ๋/๋น, ํ๋ฆฌ๊ณ ๋
//-- tmn → ์ต์ ์จ๋ ํ๊ทธ
// (5 → 5โ)
//-- tmx → ์ต๊ณ ์จ๋ ํ๊ทธ
// (21 → 21โ)
//-- rnSt → ๊ฐ์ ํ๋ฅ ํ๊ทธ
// (40 → 40%)
//-- img → ๋ ์จ ์๋ณด์ ๋ฐ๋ฅธ ์ด๋ฏธ์ง
// getter / setter ๊ตฌ์ฑ
public String getTmEf()
{
return tmEf;
}
public void setTmEf(String tmEf)
{
this.tmEf = tmEf;
}
public String getWf()
{
return wf;
}
public void setWf(String wf)
{
this.wf = wf;
}
public String getTmn()
{
return tmn;
}
public void setTmn(String tmn)
{
this.tmn = tmn;
}
public String getTmx()
{
return tmx;
}
public void setTmx(String tmx)
{
this.tmx = tmx;
}
public String getRnSt()
{
return rnSt;
}
public void setRnSt(String rnSt)
{
this.rnSt = rnSt;
}
public String getImg()
{
return img;
}
public void setImg(String img)
{
this.img = img;
}
}
weatherDAO.java
/*=========================================================================
WeatherDAO.java
- DAO ๊ตฌ์ฑ
- XML DOM ํ์ฉ → ์๊ฒฉ XML ์ฝ์ด๋ด๊ธฐ
(http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=108)
===========================================================================*/
package com.test;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class WeatherDAO
{
// ๊ณตํต ๋ฉค๋ฒ ๊ตฌ์ฑ
private Document xmlObj;
private XPath xPath;
private HashMap<String, String> map;
// ์์ฑ์ ์ ์ → ๊ธฐ๋ณธ ์์ฑ์
public WeatherDAO() throws ParserConfigurationException, IOException, SAXException
{
this("108"); // ์ ๊ตญ ๊ธฐ์ค
/*
stnId=108 : ์ ๊ตญ
stnId=109 : ์์ธ. ๊ฒฝ๊ธฐ๋
stnId=105 : ๊ฐ์๋
stnId=131 : ์ถฉ๋ถ
stnId=133 : ์ถฉ๋จ
stnId=146 : ์ ๋ถ
stnId=156 : ์ ๋จ
stnId=143 : ๊ฒฝ๋ถ
stnId=159 : ๊ฒฝ๋จ
stnId=184 : ์ ์ฃผํน๋ณ์์น๋
*/
}
// ์์ฑ์ ์ ์ → ๋งค๊ฐ๋ณ์ ์๋ ์์ฑ์
public WeatherDAO(String stnId) throws ParserConfigurationException, IOException, SAXException
{
map = new HashMap<String, String>();
map.put("๋ง์", "W_DB01.png");
map.put("ํ๋ฆผ", "W_DB04.png");
map.put("๋น", "W_DB05.png");
map.put("๊ตฌ๋ฆ์กฐ๊ธ", "W_NB02.png");
map.put("๊ตฌ๋ฆ๋ง์", "W_NB03.png");
map.put("ํ๋ฆฌ๊ณ ๋น", "W_NB08.png");
map.put("๊ตฌ๋ฆ๋ง๊ณ ๋น", "W_NB20.png");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
// DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
String str = String.format("http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=%s", stnId);
URL url = new URL(str);
InputSource is = new InputSource(url.openStream());
xmlObj = builder.parse(is);
xPath = XPathFactory.newInstance().newXPath(); //-- check~!!
//-- xPathFactory xFactory = XPathFactory.newInstance();
//xPath = xFactory.newXPath();
/*
โ XPath ์์ฑ
- XPathFactory ์ ์ ์ (static) ๋ฉ์๋ ใnewInstance()ใ ํธ์ถ์ ํตํด
XPath ๋ฅผ ์์ฑํด์ฃผ๋ XPathFactory ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ
- ์ด XPathFactory ์ ์ ์ (static) ๋ฉ์๋ ใnewXPathใ ํธ์ถ์ ํตํด
XPath ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
โ ๋
ธ๋ ์ ํ(Selection Nodes)
- ๋ธ๋ผ์ฐ์ ๋ง๋ค XPath ๋ฅผ ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ์์ ์ฐจ์ด๋ฅผ ๋ณด์ธ๋ค.
- Chrome, Firefox, Edge, Opera, Safari ๋ฑ์
ใevaluation()ใ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋
ธ๋๋ฅผ ์ฒ๋ฆฌํ๋ค.
→ xmlDoc.evaluation(xpath, xmlDoc, null, XPathResult.ANY_TYPE, null)
- IE ๋ ใselectNodes()ใ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋
ธ๋๋ฅผ ์ ํํ๋ค.
โ XPath ์ ใcompile(XPath ๊ฒฝ๋ก ํํ์)ใ
- XML ๋ฐ์ดํฐ Parsing
1. XML ์ด ์ ๊ณต๋๋ URL ๋ก ์ ์ํ์ฌ ๋ฐ์ดํฐ๋ฅผ ์์ ํ๋ค.
2. DocumentBuilderFactory ... new Instance() ๋ก factory ๋ฅผ ์์ฑํ๋ค.
3. DocumentBuilder ... new DocumentBuilder() ๋ก builder ๋ฅผ ์์ฑํ๋ค.
4. InputSource is ... new InputSource() ๋ก InputSource ๋ฅผ ์์ฑํ๋ค.
์ด ๋, ํ์ผ๋ก ์์ ํ ๊ฒฝ์ฐ๋ผ๋ฉด File ๊ฐ์ฒด๋ฅผ ๋๊ฒจ์ค๋ค.
5. Document xmlObj = builder.parse(is) ๋ก XML ํ์ผ์ ํ์ฑ(Parsing)ํ๋ค.
6. XPath xPath = XPathFactory.newInstance().newXPath()๋ก
XPath ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ
7. XPathExpression expr = XPath.compile( XPath ๊ฒฝ๋ก ํํ์) ์ผ๋ก
๊ฐ์ ธ์ฌ Element ๋ฅผ ์ ํํ๊ฒ ๋๋ค.
8. ํด๋น ๋
ธ๋ (Element) ์ ์ ๊ทผํ์ฌ ํ์ํ ๋ฐ์ดํฐ๋ฅผ ์ถ์ถํ๋ค.
*/
}
// <title>์ ๊ตญ ์ก์ ์ค๊ธฐ์๋ณด - 2024๋
02์ 05์ผ (์)์์ผ 06:00 ๋ฐํ</title>
public String weatherTitle() throws XPathExpressionException
{
String result = "";
result = xPath.compile("/rss/channel/item/title").evaluate(xmlObj);
return result;
}
/*
<wf>
<![CDATA[ โ (ํ๋์ํ) 8์ผ(๋ชฉ)~10์ผ(ํ )์ ์ ๊ตญ์ด ๋์ฒด๋ก ๋ง๊ฒ ์ผ๋, ์ ์ฃผ๋๋ ๊ตฌ๋ฆ๋ง๊ฒ ๊ณ , 11์ผ(์ผ)~15์ผ(๋ชฉ)์ ๊ตฌ๋ฆ๋ง๊ฑฐ๋ ํ๋ฆฐ ๋ ์ด ๋ง๊ฒ ์ต๋๋ค.<br />โ (๊ธฐ์จ) ์ด๋ฒ ์๋ณด๊ธฐ๊ฐ ์์นจ ๊ธฐ์จ์ -6~8๋, ๋ฎ ๊ธฐ์จ์ 3~14๋๋ก ํ๋
(์ต์ ๊ธฐ์จ -8~2๋, ์ต๊ณ ๊ธฐ์จ 3~11๋)๊ณผ ๋น์ทํ๊ฑฐ๋ ์กฐ๊ธ ๋๊ฒ ์ต๋๋ค.<br />โ (์ฃผ๋ง์ ๋ง) 10์ผ(ํ )์ ์ ๊ตญ์ด ๋์ฒด๋ก ๋ง๊ฒ ์ผ๋, 11์ผ(์ผ)์ ์ ๊ตญ์ด ๊ตฌ๋ฆ๋ง๊ฒ ์ต๋๋ค. ์์นจ ๊ธฐ์จ์ -5~2๋, ๋ฎ ๊ธฐ์จ์ 5~10๋๊ฐ ๋๊ฒ ์ต๋๋ค. ]]>
</wf>
*/
public String weatherInfo() throws XPathExpressionException
{
String result = "";
result = xPath.compile("/rss/channel/item/description/header/wf").evaluate(xmlObj);
return result;
}
/*
โป XPath ์ ใevalute()ใ ๋ฉ์๋ ๋ ๋ฒ์งธ ํ๋ผ๋ฏธํฐ → QName
- XPathContants.NODESET
- XPathContants.NODE
- XPathContants.BOOLEAN
- XPathContants.NUMBER
- XPathContants.STRING
*/
// check~!!!
// ๋์ ์ด๋ฆ ๋ฐฐ์ด ๊ตฌ์ฑ
public ArrayList<String> weatherCityList() throws XPathExpressionException
{
ArrayList<String> result = new ArrayList<String>();
NodeList cityNodeList = (NodeList)xPath.compile("/rss/channel/item/description/body/location/city").evaluate(xmlObj, XPathConstants.NODESET);
for (int i = 0; i < cityNodeList.getLength(); i++)
{
Node cityNode = cityNodeList.item(i);
result.add(cityNode.getTextContent());
}
return result;
}
// check~!!!
// ๋ ์จ ์ ๋ณด ๋ฆฌ์คํธ ๊ตฌ์ฑ
public ArrayList<WeatherDTO> weatherList(String idx) throws XPathExpressionException //-- idx → location
{
ArrayList<WeatherDTO> result = new ArrayList<WeatherDTO>();
NodeList dataNodeList = (NodeList)xPath
.compile(String.format("/rss/channel/item/description/body/location[%s]/data",idx))
.evaluate(xmlObj, XPathConstants.NODESET);
// check~!!! → ใi=1ใ, ใ<=ใ
for (int i = 1; i <=dataNodeList.getLength(); i++)
{
// tmEf
String tmEf = xPath
.compile(String.format("/rss/channel/item/description/body/location[%s]/data[%s]/tmEf"
, idx, i)).evaluate(xmlObj);
// wf
String wf = xPath
.compile(String.format("/rss/channel/item/description/body/location[%s]/data[%s]/wf"
, idx, i)).evaluate(xmlObj);
// tmn
String tmn = xPath
.compile(String.format("/rss/channel/item/description/body/location[%s]/data[%s]/tmn"
, idx, i)).evaluate(xmlObj);
// tmx
String tmx = xPath
.compile(String.format("/rss/channel/item/description/body/location[%s]/data[%s]/tmx"
, idx, i)).evaluate(xmlObj);
// rnSt
String rnSt = xPath
.compile(String.format("/rss/channel/item/description/body/location[%s]/data[%s]/rnSt"
, idx, i)).evaluate(xmlObj);
WeatherDTO w = new WeatherDTO();
w.setTmEf(tmEf);
w.setWf(wf);
w.setTmn(tmn);
w.setTmx(tmx);
w.setRnSt(rnSt);
// img
w.setImg(map.get(wf));
result.add(w);
}
return result;
}
}
weather.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>๊ธฐ์์ฒญ ์ก์ ์ค๊ธฐ ์๋ณด(Weather.jsp)</title>
<link rel="stylesheet" type="text/css" href="<%=cp %>/css/main.css">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" >
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>
๊ธฐ์ ์ ๋ณด <small>์ค๊ธฐ ์๋ณด</small>
</h2>
<div class="panel-group" role="group">
<div class="panel panel-default" role="group" >
<div class="panel-heading">์ง์ญ ์ ํ</div>
<div class="panel-body">
<form method="get" role="form">
<input type="radio" name="stnId" value="108" checked="checked"> ์ ๊ตญ
<input type="radio" name="stnId" value="109" > ์์ธ, ๊ฒฝ๊ธฐ
<input type="radio" name="stnId" value="105" > ๊ฐ์
<input type="radio" name="stnId" value="131" > ์ถฉ๋ถ
<input type="radio" name="stnId" value="133" > ์ถฉ๋จ
<input type="radio" name="stnId" value="146" > ์ ๋ถ
<input type="radio" name="stnId" value="156" > ์ ๋จ
<input type="radio" name="stnId" value="143" > ๊ฒฝ๋ถ
<input type="radio" name="stnId" value="159" > ๊ฒฝ๋จ
<input type="radio" name="stnId" value="184" > ์ ์ฃผํน๋ณ์์น๋
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</div>
<br>
<div class="panel panel-default" role="group">
<div class="panel-heading">๊ธฐ์ ์ ๋ณด ์ถ๋ ฅ</div>
<div class="panel-body">
<p>
<b>์์ธ, ๊ฒฝ๊ธฐ๋ ์ก์์ค๊ธฐ์๋ณด - 2024๋
02์ 05์ผ (์)์์ผ 06:00 ๋ฐํ</b>
</p>
<p>
โ (ํ๋์ํ) 11์ผ(์ผ), 14์ผ(์)~15์ผ(๋ชฉ)์ ๋์ฒด๋ก ํ๋ฆฌ๊ฑฐ๋ ๊ตฌ๋ฆ๋ง๊ฒ ๊ณ , ๊ทธ ๋ฐ์ ๋ ์ ๋์ฒด๋ก ๋ง๊ฒ ์ต๋๋ค.<br />
โ (๊ธฐ์จ) ์์นจ ๊ธฐ์จ์ -7~4๋, ๋ฎ ๊ธฐ์จ์ 3~10๋๋ก ํ๋
(์ต์ ๊ธฐ์จ -9~-3๋, ์ต๊ณ ๊ธฐ์จ 3~6๋)๊ณผ ๋น์ทํ๊ฑฐ๋ ๋๊ฒ ์ต๋๋ค.<br />
โ (ํด์) ์ํด์ค๋ถํด์์ ๋ฌผ๊ฒฐ์ 0.5~2.5m๋ก ์ผ๊ฒ ์ต๋๋ค.<br />
โ (์ฃผ๋ง์ ๋ง) 10์ผ(ํ )์ ๋์ฒด๋ก ๋ง๊ฒ ๊ณ , 11์ผ(์ผ)์ ๊ตฌ๋ฆ๋ง๊ฒ ์ต๋๋ค. ์์นจ ๊ธฐ์จ์ -5~-1๋, ๋ฎ ๊ธฐ์จ์ 5~7๋๊ฐ ๋๊ฒ ์ต๋๋ค.
</p>
<h3>์์ธ</h3>
<table class="table">
<thead>
<tr>
<th>๋ ์ง</th>
<th>๋ ์จ</th>
<th>์ต์ /์ต๊ณ ๊ธฐ์จ</th>
<th>๊ฐ์ํ๋ฅ </th>
</tr>
</thead>
<tbody>
<tr>
<td>2024-02-08 00:00</td>
<td>๋ง์</td>
<td>-3 ~ 5</td>
<td>10</td>
</tr>
<tr>
<td>2024-02-08 12:00</td>
<td>๊ตฌ๋ฆ๋ง์</td>
<td>-3 ~ 5</td>
<td>30</td>
</tr>
<tr>
<td>2024-02-09 00:00</td>
<td>๋ง์</td>
<td>-3 ~ 5</td>
<td>10</td>
</tr>
<tr>
<td>2024-02-09 12:00</td>
<td>๋ง์</td>
<td>-3 ~ 4</td>
<td>10</td>
</tr>
</tbody>
</table>
<h3>์ธ์ฒ</h3>
<table class="table">
<thead>
<tr>
<th>๋ ์ง</th>
<th>๋ ์จ</th>
<th>์ต์ /์ต๊ณ ๊ธฐ์จ</th>
<th>๊ฐ์ํ๋ฅ </th>
</tr>
</thead>
<tbody>
<tr>
<td>2024-02-08 00:00</td>
<td>๋ง์</td>
<td>-4 ~ 3</td>
<td>10</td>
</tr>
<tr>
<td>2024-02-08 12:00</td>
<td>๊ตฌ๋ฆ๋ง์</td>
<td>-4 ~ 3</td>
<td>30</td>
</tr>
<tr>
<td>2024-02-09 00:00</td>
<td>๋ง์</td>
<td>-3 ~ 4</td>
<td>30</td>
</tr>
<tr>
<td>2024-02-09 12:00</td>
<td>๋ง์</td>
<td>-3 ~ 4</td>
<td>10</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</body>
</html>
weatherInfo.jsp
<%@page import="com.test.WeatherDTO"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.test.WeatherDAO"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
%>
<%
// ์ด์ ํ์ด์ง(WeatherInfo.jsp) ๋ก๋ถํฐ ๋์ด์จ ๋ฐ์ดํฐ ์์
//-- stnId
String stnId = request.getParameter("stnId");
if (stnId == null)
stnId = "108"; //-- ์ ๊ตญ ๋ ์จ ์ ๋ณด
StringBuffer sb = new StringBuffer();
//WeatherDAO dao = new WeatherDAO(); // ๊ธฐ๋ณธ์์ฑ์ (stnId=108)
WeatherDAO dao = new WeatherDAO(stnId); // ๋งค๊ฐ๋ณ์ ์๋ ์์ฑ์ (stnId=๋ด๊ฐ ์ ํํ ๋ฒํธ)
// ํ์ดํ
String title = dao.weatherTitle();
// ์ก์ ์ค๊ธฐ ์๋ณด
String weatherInfo = dao.weatherInfo();
// ๋์ ์ ๋ณด ๋ฐ ๋ ์ง ์๊ฐ ๋ณ ๋ ์จ ์ ๋ณด
ArrayList<String> cityList = dao.weatherCityList();
for(int i=0; i<cityList.size(); i++)
{
sb.append(String.format("<h3>%s</h3>", cityList.get(i)));
ArrayList<WeatherDTO> weatherList = dao.weatherList(String.valueOf(i+1));
//System.out.println(weatherList.size());
// ํ
์ด๋ธ ๋์ ์์ฑ ๋ฌธ์์ด ๊ตฌ์ฑ
sb.append("<table class='table'>");
sb.append("<tr>");
sb.append("<th>๋ ์ง</th>");
sb.append("<th>๋ ์จ</th>");
sb.append("<th>์ต์ /์ต๊ณ ๊ธฐ์จ</th>");
sb.append("<th>๊ฐ์ํ๋ฅ </th>");
sb.append("</tr>");
for (WeatherDTO w : weatherList)
{
sb.append("<tr>");
sb.append(String.format("<td>%s</td>", w.getTmEf())); // ๋ ์ง ์๊ฐ
sb.append(String.format("<td><img src='images/%s'> %s</td>",w.getImg(), w.getWf())); // ๋ ์จ
sb.append(String.format("<td>%sโ ~ %sโ</td>", w.getTmn(), w.getTmx())); // ์ต์ ~ ์ต๊ณ ๊ธฐ์จ
sb.append(String.format("<td>%s%%</td>", w.getRnSt())); // ๊ฐ์ํ๋ฅ
sb.append("</tr>");
}
sb.append("</table>");
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>๊ธฐ์์ฒญ ์ก์ ์ค๊ธฐ ์๋ณด(WeatherInfo.jsp)</title>
<link rel="stylesheet" type="text/css" href="<%=cp %>/css/main.css">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" >
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
// ํ
์คํธ
//alert("ํ์ธ");
// ํ์ด์ง ์ต์ด ์์ฒญ ์ดํ
// ์ ํํ ๋ผ๋์ค ๋ฒํผ์ ์ํ๋ฅผ ์ ํ๋ ์ํ๋ก(checked) ์ ์งํ ์ ์๋๋ก ์ฒ๋ฆฌ
// ๊ด์ฐฐ
//$(":radio:eq(0)").attr("checked", "checked");
//
//$(":radio:eq(2)").attr("checked", "checked");
//
//$(":radio:eq(9)").attr("checked", "checked");
//
// ๊ด์ฐฐ
//$(":radio[value='109']").attr("checked", "checked");
//
//$(":radio[value='156']").attr("checked", "checked");
//
//$(":radio[value='143']").attr("checked", "checked");
$(":radio[value='<%=stnId%>']").attr("checked", "checked");
})
</script>
</head>
<body>
<div class="container">
<h2>
๊ธฐ์ ์ ๋ณด <small>์ค๊ธฐ ์๋ณด</small>
</h2>
<div class="panel-group" role="group">
<div class="panel panel-default" role="group" >
<div class="panel-heading">์ง์ญ ์ ํ</div>
<div class="panel-body">
<!-- action ์์ฑ ์๋ต → ์์ฒญ ๋ฐ ์์ ์ฒ๋ฆฌ๋ ์๊ธฐ ์์ ~!!! -->
<form method="get" role="form">
<input type="radio" name="stnId" value="108" checked="checked"> ์ ๊ตญ
<input type="radio" name="stnId" value="109" > ์์ธ, ๊ฒฝ๊ธฐ
<input type="radio" name="stnId" value="105" > ๊ฐ์
<input type="radio" name="stnId" value="131" > ์ถฉ๋ถ
<input type="radio" name="stnId" value="133" > ์ถฉ๋จ
<input type="radio" name="stnId" value="146" > ์ ๋ถ
<input type="radio" name="stnId" value="156" > ์ ๋จ
<input type="radio" name="stnId" value="143" > ๊ฒฝ๋ถ
<input type="radio" name="stnId" value="159" > ๊ฒฝ๋จ
<input type="radio" name="stnId" value="184" > ์ ์ฃผํน๋ณ์์น๋
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</div>
<br>
<div class="panel panel-default" role="group">
<div class="panel-heading">๊ธฐ์ ์ ๋ณด ์ถ๋ ฅ</div>
<div class="panel-body">
<p>
<!-- <b>์์ธ, ๊ฒฝ๊ธฐ๋ ์ก์์ค๊ธฐ์๋ณด - 2024๋
02์ 05์ผ (์)์์ผ 06:00 ๋ฐํ</b> -->
<b><%=title %></b>
</p>
<p style="font-size: 10pt;">
<!--
โ (ํ๋์ํ) 11์ผ(์ผ), 14์ผ(์)~15์ผ(๋ชฉ)์ ๋์ฒด๋ก ํ๋ฆฌ๊ฑฐ๋ ๊ตฌ๋ฆ๋ง๊ฒ ๊ณ , ๊ทธ ๋ฐ์ ๋ ์ ๋์ฒด๋ก ๋ง๊ฒ ์ต๋๋ค.<br />
โ (๊ธฐ์จ) ์์นจ ๊ธฐ์จ์ -7~4๋, ๋ฎ ๊ธฐ์จ์ 3~10๋๋ก ํ๋
(์ต์ ๊ธฐ์จ -9~-3๋, ์ต๊ณ ๊ธฐ์จ 3~6๋)๊ณผ ๋น์ทํ๊ฑฐ๋ ๋๊ฒ ์ต๋๋ค.<br />
โ (ํด์) ์ํด์ค๋ถํด์์ ๋ฌผ๊ฒฐ์ 0.5~2.5m๋ก ์ผ๊ฒ ์ต๋๋ค.<br />
โ (์ฃผ๋ง์ ๋ง) 10์ผ(ํ )์ ๋์ฒด๋ก ๋ง๊ฒ ๊ณ , 11์ผ(์ผ)์ ๊ตฌ๋ฆ๋ง๊ฒ ์ต๋๋ค. ์์นจ ๊ธฐ์จ์ -5~-1๋, ๋ฎ ๊ธฐ์จ์ 5~7๋๊ฐ ๋๊ฒ ์ต๋๋ค.
-->
<%=weatherInfo %>
</p>
<!--
<h3>์์ธ</h3>
<table class="table">
<thead>
<tr>
<th>๋ ์ง</th>
<th>๋ ์จ</th>
<th>์ต์ /์ต๊ณ ๊ธฐ์จ</th>
<th>๊ฐ์ํ๋ฅ </th>
</tr>
</thead>
<tbody>
<tr>
<td>2024-02-08 00:00</td>
<td>๋ง์</td>
<td>-3 ~ 5</td>
<td>10</td>
</tr>
<tr>
<td>2024-02-08 12:00</td>
<td>๊ตฌ๋ฆ๋ง์</td>
<td>-3 ~ 5</td>
<td>30</td>
</tr>
<tr>
<td>2024-02-09 00:00</td>
<td>๋ง์</td>
<td>-3 ~ 5</td>
<td>10</td>
</tr>
<tr>
<td>2024-02-09 12:00</td>
<td>๋ง์</td>
<td>-3 ~ 4</td>
<td>10</td>
</tr>
</tbody>
</table>
<h3>์ธ์ฒ</h3>
<table class="table">
<thead>
<tr>
<th>๋ ์ง</th>
<th>๋ ์จ</th>
<th>์ต์ /์ต๊ณ ๊ธฐ์จ</th>
<th>๊ฐ์ํ๋ฅ </th>
</tr>
</thead>
<tbody>
<tr>
<td>2024-02-08 00:00</td>
<td>๋ง์</td>
<td>-4 ~ 3</td>
<td>10</td>
</tr>
<tr>
<td>2024-02-08 12:00</td>
<td>๊ตฌ๋ฆ๋ง์</td>
<td>-4 ~ 3</td>
<td>30</td>
</tr>
<tr>
<td>2024-02-09 00:00</td>
<td>๋ง์</td>
<td>-3 ~ 4</td>
<td>30</td>
</tr>
<tr>
<td>2024-02-09 12:00</td>
<td>๋ง์</td>
<td>-3 ~ 4</td>
<td>10</td>
</tr>
</tbody>
</table>
-->
<%=sb.toString() %>
</div>
</div>
</div>
</div>
</body>
</html>
[XmlApp 09]
newsDAO.java
/*====================
NewsDAO.java
=====================*/
package com.test;
import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class NewsDAO
{
private Document xmlObj;
private XPath xPath;
public NewsDAO(String category) //throws ParserConfigurationException, IOException, SAXException
{
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
String str = String.format("https://www.chosun.com/arc/outboundfeeds/rss/category/%s/?outputType=xml", category);
URL url = new URL(str);
InputSource is = new InputSource(url.openStream());
xmlObj = builder.parse(is);
xPath = XPathFactory.newInstance().newXPath();
} catch (Exception e)
{
System.out.println(e.toString());
}
}
public String img() throws XPathExpressionException
{
String result = "";
result = xPath.compile("/rss/channel/image/url").evaluate(xmlObj);
return result;
}
// ๊ธฐ์ฌ ๋ด์ฉ ๋ฆฌ์คํธ ๊ตฌ์ฑ
public ArrayList<NewsDTO> newsContent() throws XPathExpressionException
{
ArrayList<NewsDTO> result = new ArrayList<NewsDTO>();
NodeList newsContentNodeList = (NodeList)xPath.compile("/rss/channel/item").evaluate(xmlObj, XPathConstants.NODESET);
for (int i = 0; i < newsContentNodeList.getLength(); i++)
{
String title = xPath.compile
(String.format("/rss/channel/item[%s]/title", i)).evaluate(xmlObj);
String pub = xPath.compile
(String.format("/rss/channel/item[%s]/pubDate", i)).evaluate(xmlObj);
String content = xPath.compile
(String.format("/rss/channel/item[%s]/encoded", i)).evaluate(xmlObj);
NewsDTO dto = new NewsDTO();
dto.setTitle(title);
dto.setPubDate(pub);
dto.setContent(content);
result.add(dto);
}
return result;
}
}
newsDTO.java
/*====================
NewsDTO.java
=====================*/
package com.test;
public class NewsDTO
{
private String title, content, pubDate;
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public String getContent()
{
return content;
}
public void setContent(String content)
{
this.content = content;
}
public String getPubDate()
{
return pubDate;
}
public void setPubDate(String pubDate)
{
this.pubDate = pubDate;
}
}
newsInfo.jsp
<%@page import="com.test.NewsDTO"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.test.NewsDAO"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
%>
<%
String category = request.getParameter("category");
String img = request.getParameter("img");
if (category==null)
{
category="politics";
}
NewsDAO dao = new NewsDAO(category);
StringBuffer sb = new StringBuffer();
ArrayList<NewsDTO> newsContent = dao.newsContent();
sb.append("<table class='table'>");
sb.append("<tr>");
sb.append("<th>์ ๋ชฉ</th>");
sb.append("<th>๋ ์ง</th>");
sb.append("<th>๋ด์ฉ</th>");
sb.append("</tr>");
for(NewsDTO dto : newsContent)
{
sb.append("<tr>");
sb.append(String.format("<td style='width:200px; '>%s</td>", dto.getTitle()));
sb.append(String.format("<td style='width:200px; '>%s</td>", dto.getPubDate()));
sb.append(String.format("<td style='width:200px; '>%s</td>", dto.getContent()));
sb.append("</tr>");
}
sb.append("</table>");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>NewsInfo.jsp</title>
<link rel="stylesheet" type="text/css" href="<%=cp %>/css/main.css">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" >
<!-- ๋ถํธ์คํธ๋ฉ ์ ์ฉ CSS -->
<link rel="stylesheet" type="text/css"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<!-- ๋ถํธ์คํธ๋ฉ ๋ถ๊ฐ ํ
๋ง CSS -->
<link rel="stylesheet" type="text/css"
href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css">
<!-- ์ ์ด์ฟผ๋ฆฌ ์ ์ฉ JS -->
<script type="text/javascript"
src="http://code.jquery.com/jquery.min.js"></script>
<!-- ๋ถํธ์คํธ๋ฉ ๊ด๋ จ ์คํฌ๋ฆฝํธ ์ ์ฉ JS -->
<script type="text/javascript"
src="http://maxcdn.bootstrap.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script type="text/javascript">
<%-- $(document).ready(function()
{
$(":radio[value='<%=category%>']").attr("checked", "checked");
}) --%>
</script>
<style type="text/css">
img {width: 416px; height: 207px;}
.container {
width: 80%;
margin: 0 auto;
padding: 20px;
font-family: ๋ง์ ๊ณ ๋;
}
h2 {
color: #333;
}
.panel {
margin-top: 20px;
}
.panel-heading {
width: 100%;
background-color: #337ab7;
color: #fff;
padding: 10px;
margin-right: 20px;
font-family: ๋ง์ ๊ณ ๋;
}
input[type="radio"] {
margin-right: 20px;
}
.btn-default {
width: 100%;
background-color: #337ab7;
color: #fff;
padding: 10px 15px;
border: none;
cursor: pointer;
}
.panel-body {
width: 100%;
margin-top: 10px;
border: 1px solid #ddd;
padding: 15px;
font-family: ๋ง์ ๊ณ ๋;
}
form input[type="radio"] {
margin-right: 10px;
}
label {
display: inline-block;
margin-bottom: 10px;
}
form div {
margin-bottom: 10px;
}
</style>
</head>
<body>
<div style="text-align: center; margin-bottom: 30px; border-radius: 30px;">
<img src="https://www.chosun.com/resizer/tMofBBp8RA5HaB4tbxnftjdS9qA=/image.chosun.com/cs_logo.png">
</div>
<div style="text-align: center; width: 100%; font-size: 30px; margin-left: 70px;
background-color: #fff; border-color: #222;">
<nav class="nav nav-pills nav-justified " style="color: black;">
<a class="nav-link active" aria-current="page" href="http://localhost:3306/XmlApp09/NewsInfo.jsp?category=national">์ ์น</a>
<a class="nav-link" href="http://localhost:3306/XmlApp09/NewsInfo.jsp?category=economy">๊ฒฝ์ </a>
<a class="nav-link" href="http://localhost:3306/XmlApp09/NewsInfo.jsp?category=national#">์ฌํ</a>
<a class="nav-link" href="http://localhost:3306/XmlApp09/NewsInfo.jsp?category=international">๊ตญ์ </a>
<a class="nav-link" href="http://localhost:3306/XmlApp09/NewsInfo.jsp?category=culture-life">๋ฌธํ/๋ผ์ดํ</a>
<a class="nav-link" href="http://localhost:3306/XmlApp09/NewsInfo.jsp?category=opinion">์คํผ๋์ธ</a>
<a class="nav-link" href="http://localhost:3306/XmlApp09/NewsInfo.jsp?category=sports">์คํฌ์ธ </a>
<a class="nav-link" href="http://localhost:3306/XmlApp09/NewsInfo.jsp?category=entertainments">์ฐ์</a>
</nav>
</div>
<div class="container">
<!--
<div class="panel panel-default" role="group">
<div class="panel panel-heading">๊ธฐ์ฌ ์นดํ
๊ณ ๋ฆฌ</div>
<div class="panel panel-body">
<form method="get" role="form">
<input type="radio" name="category" value="politics" checked="checked">์ ์น
<input type="radio" name="category" value="economy">๊ฒฝ์
<input type="radio" name="category" value="national">์ฌํ
<input type="radio" name="category" value="international">๊ตญ์ <br>
<input type="radio" name="category" value="culture-life">๋ฌธํ/๋ผ์ดํ
<input type="radio" name="category" value="opinion">์คํผ๋์ธ
<input type="radio" name="category" value="sports">์คํฌ์ธ
<input type="radio" name="category" value="entertainments">์ฐ์
<br>
<button type="submit" class="btn btn-default">์นดํ
๊ณ ๋ฆฌ ์ ํ</button>
</form>
</div>
-->
<div class="panel panel-default" role="group">
<div class="panel panel-heading" style="text-align: center; font-weight: bold; font-size: 30px;" >๊ธฐ์ฌ ๋ด์ฉ</div>
<div class="panel panel-body">
<%=sb.toString() %>
</div>
</div>
</div>
</div>
</body>
</html>
'XmlStudy > XmlApp' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
โ XmlTest 01 ~ 04 [xml ํ์ผ] (0) | 2024.01.31 |
---|