Форум программистов, компьютерный форум, киберфорум
Java
Войти
Регистрация
Восстановить пароль
Карта форума Темы раздела Блоги Сообщество Поиск Заказать работу  
 
Рейтинг 5.00/5: Рейтинг темы: голосов - 5, средняя оценка - 5.00
0 / 0 / 0
Регистрация: 08.02.2015
Сообщений: 26
1

Unmarshalling

17.10.2015, 15:33. Показов 864. Ответов 1
Метки нет (Все метки)

Author24 — интернет-сервис помощи студентам
Добрый день,
мое изучение 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 и вывести в консоль
Хелп с массивами.
Спасибо
0
Programming
Эксперт
94731 / 64177 / 26122
Регистрация: 12.04.2006
Сообщений: 116,782
17.10.2015, 15:33
Ответы с готовыми решениями:

Xml Java Unmarshalling
Доброго времени суток. сижу уже битый 3 час с этой штукой. все вроде норм, скомпилил. но не...

Parcel Unmarshalling unknown type code 7667826 at offset 248
При передаче одного значения ArrayList из Activity в Activity все проходит хорошо, но при &lt;2...


Искать еще темы с ответами

Или воспользуйтесь поиском по форуму:
1
944 / 687 / 230
Регистрация: 28.04.2013
Сообщений: 1,926
17.10.2015, 18:27 2
AlenaE,


что бы избавится от Row@eed1f14 в классе Row переопределить метод toString().. а дальше нужно посмотереть..

Добавлено через 1 час 24 минуты
AlenaE,

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
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 {
 
        File file = new File("row.xml"); //указать правильный путь к файлу
 
        JAXBContext jaxbContext = JAXBContext.newInstance(Exchangerates.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
 
        Exchangerates result = (Exchangerates) jaxbUnmarshaller.unmarshal(file);
 
        for (Row row :result.getRows()) {
            System.out.println(row.getExchangerate());
        }
    }
}
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
76
import javax.xml.bind.annotation.*;
 
/**
 *
 * @author ALENA
 */
//https://api.privatbank.ua/api-info/exchangerate.html
 
@XmlRootElement
public class Exchangerate {
 
 
    private String ccy;
    private String buy;
    private String sale;
    private String base_ccy;
 
 
    public Exchangerate() {
    }
 
    public Exchangerate(String ccy, String buy, String sale, String base_ccy) {
        this.ccy = ccy;
        this.buy = buy;
        this.sale = sale;
        this.base_ccy = base_ccy;
    }
 
 
    @XmlAttribute (name = "ccy")
    public void setCcy(String ccy) {
        this.ccy = ccy;
    }
 
 
    @XmlAttribute (name = "buy")
    public void setBuy(String buy) {
 
        this.buy = buy;
    }
 
    @XmlAttribute (name = "sale")
    public void setSale(String sale) {
        this.sale = sale;
    }
 
 
    @XmlAttribute (name = "base_ccy")
    public void setBase_ccy(String base_ccy) {
        this.base_ccy = base_ccy;
    }
 
    public String getCcy() {
        return ccy;
    }
 
    public String getBuy() {
        return buy;
    }
 
    public String getSale() {
        return sale;
    }
 
    public String getBase_ccy() {
        return base_ccy;
    }
 
    @Override
    public String toString() {
        return "ccy = " + this.ccy +
                "\tbase ccy = " + this.base_ccy +
                "\tbuy = " + this.buy +
                "\t sale = " + this.sale;
    }
}
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 javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
 
/**
 * Created by reisal78 on 17.10.15.
 */
@XmlRootElement
public class Row {
    private Exchangerate exchangerate;
 
    public Row() {
    }
 
    public Row(Exchangerate exchangerate) {
        this.exchangerate = exchangerate;
    }
 
    @XmlElement(name = "exchangerate")
    public void setExchangerate(Exchangerate exchangerate) {
        this.exchangerate = exchangerate;
    }
 
 
    public Exchangerate getExchangerate() {
        return exchangerate;
    }
}
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 javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
 
/**
 * Created by reisal78 on 17.10.15.
 */
@XmlRootElement
public class Row {
    private Exchangerate exchangerate;
 
    public Row() {
    }
 
    public Row(Exchangerate exchangerate) {
        this.exchangerate = exchangerate;
    }
 
    @XmlElement(name = "exchangerate")
    public void setExchangerate(Exchangerate exchangerate) {
        this.exchangerate = exchangerate;
    }
 
 
    public Exchangerate getExchangerate() {
        return exchangerate;
    }
}
1
17.10.2015, 18:27
Ответ Создать тему
КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin
Copyright ©2000 - 2024, CyberForum.ru