'프로그래밍/JAVA'에 해당되는 글 2건

Java Pair 자료구조

C++ 에는 Pair 자료구조가 있는데, 자바에는 없다. 
직접 만들어서 써야 하는 건가? 
찾아보다가 맘에 드는 소스를 보관해본다.

    class Pair<L,R> {
final L left;
final R right;

public Pair(L left, R right) {
this.left = left;
this.right = right;
}

static <L,R> Pair<L,R> of(L left, R right){
return new Pair<L,R>(left, right);
}
}

list.add(Pair.of(x,y)); // my preference 
list.add(pairOf(x,y)); // use with import static x.y.Pair.pairOf

참고 : @simbo1905 http://stackoverflow.com/questions/521171/a-java-collection-of-value-pairs-tuples


'프로그래밍 > JAVA' 카테고리의 다른 글

Web-Scraping(XML)  (0) 2017.04.06
블로그 이미지

ohnewdev

배워서 남주자

,
Web-Scraping

Web-Scraping(XML)

오늘 일하다가 웹스크랩핑 이란 것을 알게되었고,
간단히 테스트 프로그램을 만들어 보았다.

xml 페이지 읽어오기

package main;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

public class GetCurrencyInfo {

public static void main(String[] args) {
getCurRate();
}

private static void getCurRate() {
try {
// String searchUrl = "http://localhost/eurofxref-daily.xml";
String searchUrl = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml";
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(searchUrl);

// xpath 생성
XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "/*/*/*/Cube";

NodeList cols = (NodeList) xpath.compile(expression).evaluate(document, XPathConstants.NODESET);

System.out.println("=====================");
System.out.println(cols.item(0).getParentNode().getAttributes().getNamedItem("time").getNodeValue());
System.out.println("=====================");

for (int idx = 0; idx < cols.getLength(); idx++) {
String cur_rate = cols.item(idx).getAttributes().getNamedItem("currency").getNodeValue() + " " + cols.item(idx).getAttributes().getNamedItem("rate").getNodeValue();
System.out.println(cur_rate);
}
} catch (Exception e) {
e.printStackTrace();
}

}
}

구동결과

runnable jar 로 만들어서 돌려본 결과…

D:\dev_git\TestRunnable>java -jar CheckCur_local.jar
=====================
2017-04-05
=====================
USD 1.0678
JPY 118.49
BGN 1.9558
CZK 27.058
DKK 7.4354
...

[참고]
[ksah_web scraping]1
[xPath_doc_tutorial]2 


'프로그래밍 > JAVA' 카테고리의 다른 글

Java Pair 자료구조  (0) 2017.04.14
블로그 이미지

ohnewdev

배워서 남주자

,