1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| package dao.impl;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;
import org.xml.sax.XMLReader;
public class test{
public static final Namespace REF_NS =
Namespace.getNamespace("ref", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");
// Pas envie de mettre le nom de la racine, remplaçons par *
public static final String CURRENCY_PATH_TEMPLATE =
"/*/ref:Cube/ref:Cube/ref:Cube[@currency='%s']";
static org.jdom.Document document;
SAXBuilder sxb = new SAXBuilder();{
try{
document = sxb.build(new URL("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"));
}
catch(Exception e){
e.printStackTrace();}
}
static String getCurrencyValue(String currencyCode) throws JDOMException {
String sXPath = String.format(CURRENCY_PATH_TEMPLATE, currencyCode);
XPath path = XPath.newInstance(sXPath);
path.addNamespace(REF_NS);
Element cube = (Element)path.selectSingleNode(document);
return cube.getAttributeValue("rate");
}
public static void main(String[] args){
try {
getCurrencyValue("USD");
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} |