Добрый день,
мое изучение java только в процессе
Есть файл
XML |
1
2
3
4
5
6
7
8
9
10
11
12
| <?xml version="1.0" encoding="UTF-8"?>
<exchangerates>
<row>
<exchangerate ccy="RUR" base_ccy="UAH" buy="0.36000" sale="0.38000"/>
</row>
<row>
<exchangerate ccy="EUR" base_ccy="UAH" buy="25.70000" sale="26.70000"/>
</row>
<row>
<exchangerate ccy="USD" base_ccy="UAH" buy="22.65000" sale="23.45000"/>
</row>
</exchangerates> |
|
надо вывести курс
СОздала 2 класса
Java |
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
| import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
/**
*
* @author ALENA
*/
public class Client {
public static void main(String[] args) throws JAXBException {
// String path = "http://127.0.0.1:8080/simple-rest/service?id=7";
//C:\Users\ALENA\Documents\NetBeansProjects\Unmurshall\src
File file = new File("C:/Users/ALENA/Documents/NetBeansProjects/Unmurshall/src/Row.xml");
// HttpURLConnection connection = (HttpURLConnection) url.openConnection();
JAXBContext jaxbContext = JAXBContext.newInstance(Row.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Row cr = (Row) jaxbUnmarshaller.unmarshal(file);
cr.toString();
System.out.println(cr);
}
} |
|
2й класс
Java |
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
| /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
*
* @author ALENA
*/
//https://api.privatbank.ua/api-info/exchangerate.html
@XmlRootElement(name = "exchangerates")
@XmlType(propOrder={"ccy", "buy", "sale", "base_ccy","row"})
public class Row {
private String ccy;
private String buy;
private String sale;
private String base_ccy;
List<Row> row;
@XmlElement(name="row")
public List<Row> getRow() {
return row;
}
public void setRow(List<Row> row) {
this.row = row;
}
public Row() {
}
public String getCcy() {
return ccy;
}
// @XmlElement
public void setCcy(String ccy) {
this.ccy = ccy;
}
public String getBuy() {
return buy;
}
//@XmlElement
public void setBuy(String buy) {
this.buy = buy;
}
public String getSale() {
return sale;
}
// @XmlElement
public void setSale(String sale) {
this.sale = sale;
}
public String getBase_ccy() {
return base_ccy;
}
//@XmlElement
public void setBase_ccy(String base_ccy) {
this.base_ccy = base_ccy;
}
} |
|
пока результат
run:
Row@eed1f14
BUILD SUCCESSFUL (total time: 0 seconds)
Теперь надо как понимаю создать 3й класс (row нода exchangerates ) закинуть все в List и вывести в консоль
Хелп с массивами.
Спасибо