Created
November 12, 2015 13:19
-
-
Save vadymhimself/256eae2fc0a400f89f8f to your computer and use it in GitHub Desktop.
Yahoo Rates JAXB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.xml.bind.annotation.XmlElement; | |
import javax.xml.bind.annotation.XmlRootElement; | |
@XmlRootElement(name = "rate") | |
public class Rate { | |
@XmlElement(name = "Name") | |
String name; | |
@XmlElement(name = "Rate") | |
String rate; | |
@Override | |
public String toString() { | |
return "Rate for " + name + ": " + rate + "\n"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.xml.bind.JAXBContext; | |
import javax.xml.bind.Unmarshaller; | |
import java.net.URL; | |
public class Test { | |
public static void main(String[] args) { | |
try { | |
URL url = new URL("http://query.yahooapis.com/v1/public/yql?format=xml" + | |
"&q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDEUR%22,%20%22USDUAH%22)" + | |
"&env=store://datatables.org/alltableswithkeys"); | |
JAXBContext jaxbContext = JAXBContext.newInstance(Query.class); | |
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); | |
Query query = (Query) unmarshaller.unmarshal(url.openStream()); | |
System.out.println(query.results.toString()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.xml.bind.annotation.XmlElement; | |
import javax.xml.bind.annotation.XmlRootElement; | |
import java.util.ArrayList; | |
import java.util.List; | |
@XmlRootElement(name = "results") | |
public class YahooResults { | |
@XmlElement(name = "rate") | |
List<Rate> rates = new ArrayList<>(); | |
@Override | |
public String toString() { | |
StringBuilder builder = new StringBuilder("Rates [" + rates.size() + "]:\n"); | |
rates.stream().forEach(builder::append); | |
return builder.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment