Last active
October 19, 2016 09:42
-
-
Save Viacheslav77/b348bd62a9bc3cb08f10bb18d9f7c4c0 to your computer and use it in GitHub Desktop.
Написать парсер для Yahoo Finance в режиме XML (format=xml).
This file contains 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
package com.company.yahoo.finance.xml; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.PrintWriter; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import javax.xml.bind.JAXBContext; | |
import javax.xml.bind.JAXBException; | |
import javax.xml.bind.Marshaller; | |
import javax.xml.bind.Unmarshaller; | |
class BuildQuery { | |
static void performRequest(String urlStr, String path) throws IOException { | |
URL url = new URL(urlStr); | |
StringBuilder sb = new StringBuilder(); | |
HttpURLConnection http = (HttpURLConnection) url.openConnection(); | |
try (BufferedReader br = new BufferedReader(new InputStreamReader(http.getInputStream()))){ | |
String s; | |
while((s=br.readLine())!=null) | |
sb.append(s); | |
} | |
System.out.println("Полученный тектовый ответ от Yahoo в виде :\n" + "---------------------------------------------\n" + sb+"\n"); | |
File file = new File(path); | |
try(PrintWriter fwr = new PrintWriter(file)){ | |
fwr.write(sb.toString()); | |
} | |
} | |
static Query getParseRequestJABX(String path){ | |
File file = new File(path); | |
Query queryTMP = null; | |
try { | |
JAXBContext jaxbContext = JAXBContext.newInstance(Query.class); | |
Marshaller marshaller = jaxbContext.createMarshaller(); | |
// читабельное форматирование | |
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); | |
// читаем из файла | |
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); | |
queryTMP = (Query) unmarshaller.unmarshal(file); | |
System.out.println("Проверяем запарсенный XML :\n" + "---------------------------------------------"); | |
marshaller.marshal(queryTMP, System.out); | |
} catch (JAXBException e) { | |
e.printStackTrace(); | |
} | |
System.out.println("\nРаботаем с объектами :\n" + "---------------------------------------------"); | |
return queryTMP; | |
} | |
} |
This file contains 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
Полученный тектовый ответ от Yahoo в виде : | |
--------------------------------------------- | |
<?xml version="1.0" encoding="UTF-8"?> | |
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="2" yahoo:created="2016-10-18T20:07:32Z" yahoo:lang="en-US"><results><rate id="USDEUR"><Name>USD/EUR</Name><Rate>0.9105</Rate><Date>10/18/2016</Date><Time>7:56pm</Time><Ask>0.9110</Ask><Bid>0.9105</Bid></rate><rate id="USDUAH"><Name>USD/UAH</Name><Rate>25.5800</Rate><Date>10/18/2016</Date><Time>5:06pm</Time><Ask>25.8300</Ask><Bid>25.5800</Bid></rate></results></query><!-- total: 33 --> | |
<!-- prod_bf1_1;paas.yql;queryyahooapiscomproductionbf1;78e4687a-8c17-11e6-9353-d4ae5297450a --> | |
Проверяем запарсенный XML : | |
--------------------------------------------- | |
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | |
<query xmlns:ns1="http://www.yahooapis.com/v1/base.rng" ns1:count="2" ns1:created="2016-10-18T20:07:32Z" ns1:lang="en-US"> | |
<results> | |
<rate id="USDEUR"> | |
<Ask>0.911</Ask> | |
<Bid>0.9105</Bid> | |
<Date>10/18/2016</Date> | |
<Name>USD/EUR</Name> | |
<Rate>0.9105</Rate> | |
<Time>7:56pm</Time> | |
</rate> | |
<rate id="USDUAH"> | |
<Ask>25.83</Ask> | |
<Bid>25.58</Bid> | |
<Date>10/18/2016</Date> | |
<Name>USD/UAH</Name> | |
<Rate>25.58</Rate> | |
<Time>5:06pm</Time> | |
</rate> | |
</results> | |
</query> | |
Работаем с объектами : | |
--------------------------------------------- | |
[Rate{id=USDEUR,name='USD/EUR', rate=0.9105, date='10/18/2016', time='7:56pm', ask=0.911, bid=0.9105}, Rate{id=USDUAH,name='USD/UAH', rate=25.58, date='10/18/2016', time='5:06pm', ask=25.83, bid=25.58}] |
This file contains 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
package com.company.yahoo.finance.xml; | |
// Написать парсер для Yahoo Finance в режиме XML (format=xml). | |
class Main { | |
public static void main(String[] args) throws Exception { | |
String request = "http://query.yahooapis.com/v1/public/yql?format=xml&q=select%20*%20from%20" + | |
"yahoo.finance.xchange%20where%20pair%20in%20(\"USDEUR\",%20\"USDUAH\")&env=store://datatables.org/alltableswithkeys"; | |
String path = "D:\\1\\yahoo.xml"; | |
BuildQuery.performRequest(request, path); | |
Query query = BuildQuery.getParseRequestJABX(path); | |
System.out.println(query.getResults().getRate()); | |
} | |
} |
This file contains 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
package com.company.yahoo.finance.xml; | |
import javax.xml.bind.annotation.XmlAttribute; | |
import javax.xml.bind.annotation.XmlElement; | |
import javax.xml.bind.annotation.XmlRootElement; | |
@XmlRootElement(name = "query") | |
class Query { | |
private int count; | |
private String created; | |
private String lang; | |
private Results results = new Results(); | |
public int getCount() { | |
return count; | |
} | |
@XmlAttribute(namespace = "http://www.yahooapis.com/v1/base.rng") | |
public void setCount(int count) { | |
this.count = count; | |
} | |
public String getCreated() { | |
return created; | |
} | |
@XmlAttribute(namespace = "http://www.yahooapis.com/v1/base.rng") | |
public void setCreated(String created) { | |
this.created = created; | |
} | |
public String getLang() { | |
return lang; | |
} | |
@XmlAttribute(namespace = "http://www.yahooapis.com/v1/base.rng") | |
public void setLang(String lang) { | |
this.lang = lang; | |
} | |
public Results getResults() { | |
return results; | |
} | |
@XmlElement(name = "results") | |
public void setResults(Results results) { | |
this.results = results; | |
} | |
@Override | |
public String toString() { | |
return this.results.toString(); | |
} | |
} |
This file contains 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
package com.company.yahoo.finance.xml; | |
import javax.xml.bind.annotation.XmlAttribute; | |
import javax.xml.bind.annotation.XmlElement; | |
import javax.xml.bind.annotation.XmlRootElement; | |
@XmlRootElement (name="rate") | |
public class Rate { | |
private String id; | |
private String name; | |
private double rate; | |
private String date; | |
private String time; | |
private double ask; | |
private double bid; | |
public Rate(){ | |
} | |
public String getId() { | |
return id; | |
} | |
@XmlAttribute(required = true) | |
public void setId(String id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
@XmlElement(name="Name") | |
public void setName(String name) { | |
this.name = name; | |
} | |
public double getRate() { | |
return rate; | |
} | |
@XmlElement(name="Rate") | |
public void setRate(double rate) { | |
this.rate = rate; | |
} | |
public String getDate() { | |
return date; | |
} | |
@XmlElement(name="Date") | |
public void setDate(String date) { | |
this.date = date; | |
} | |
public String getTime() { | |
return time; | |
} | |
@XmlElement(name="Time") | |
public void setTime(String time) { | |
this.time = time; | |
} | |
public double getAsk() { | |
return ask; | |
} | |
@XmlElement(name="Ask") | |
public void setAsk(double ask) { | |
this.ask = ask; | |
} | |
public double getBid() { | |
return bid; | |
} | |
@XmlElement(name="Bid") | |
public void setBid(double bid) { | |
this.bid = bid; | |
} | |
@Override | |
public String toString() { | |
final StringBuilder sb = new StringBuilder("Rate{"); | |
sb.append("id=").append(id).append(",") | |
.append("name='").append(name).append('\'') | |
.append(", rate=").append(rate) | |
.append(", date='").append(date).append('\'') | |
.append(", time='").append(time).append('\'') | |
.append(", ask=").append(ask) | |
.append(", bid=").append(bid) | |
.append('}'); | |
return sb.toString(); | |
} | |
} |
This file contains 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
package com.company.yahoo.finance.xml; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.xml.bind.annotation.XmlElement; | |
import javax.xml.bind.annotation.XmlRootElement; | |
@XmlRootElement (name="results") | |
class Results { | |
private List<Rate> rate = new ArrayList<Rate>(); | |
public List<Rate> getRate() { | |
return rate; | |
} | |
@XmlElement(name = "rate") | |
public void setRate(List<Rate> rate) { | |
this.rate = rate; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment