java - Unmarshal nested Map with Jaxb -


i need following dto

@xmlrootelement(name = "exchangerate") @xmlaccessortype(xmlaccesstype.field) public class exchrates {      @xmljavatypeadapter(dateadapter.class)     private date date;      @xmljavatypeadapter(jaxbexchangeratesmapadapter.class)     private map<currencyunit, map<currencyunit, double>> rates = new hashmap<>(); } 

how can unmarshal xml dto above?

<exchangerate>     <date>2015-05-04</date>     <eur>         <eur>1</eur>         <gbp>0.73788</gbp>         <usd>1.1152</usd>     </eur>     <gbp>         <eur>1.35523</eur>         <gbp>1</gbp>         <usd>1.51136</usd>     </gbp>     <usd>         <eur>0.8967</eur>         <gbp>0.66166</gbp>         <usd>1</usd>     </usd> </exchangerate> 

i read tutorials , examples found no 1 keys node values of xml.

edit

after hours i'm close solution.

my xmladapter:

public class jaxbexchangeratesmapadapter extends xmladapter<jaxbexchangeratesmap, map<currencyunit, map<currencyunit, double>>> {      @override     public map<currencyunit, map<currencyunit, double>> unmarshal(jaxbexchangeratesmap v) throws exception {         return null;     }      @override     public jaxbexchangeratesmap marshal(map<currencyunit, map<currencyunit, double>> v) throws exception {         jaxbexchangeratesmap map = new jaxbexchangeratesmap();          (currencyunit currencyfrom : v.keyset()) {             map<currencyunit, double> = v.get(currencyfrom);             jaxbexchangeratesentry entry = new jaxbexchangeratesentry();             (currencyunit currencyto : from.keyset()) {                 entry.getentries().add(new jaxbelement<>(new qname(currencyto.getcurrencycode()), double.class, from.get(currencyto)));             }             jaxbelement<jaxbexchangeratesentry> jaxbelement = new jaxbelement<>(new qname(currencyfrom.getcurrencycode()), jaxbexchangeratesentry.class, entry);             map.getentires().add(jaxbelement);         }         return map;     }  } 

and mapped classes:

@xmlaccessortype(xmlaccesstype.field) @xmlseealso(jaxbexchangeratesentry.class) public class jaxbexchangeratesmap extends printable {      private static final long serialversionuid = 15543456767150881l;      @xmlanyelement     private list<jaxbelement<jaxbexchangeratesentry>> entires = new arraylist<>();      public list<jaxbelement<jaxbexchangeratesentry>> getentires() {         return entires;     }      public jaxbexchangeratesmap setentires(list<jaxbelement<jaxbexchangeratesentry>> entires) {         this.entires = entires;         return this;     } }  @xmlaccessortype(xmlaccesstype.field) public class jaxbexchangeratesentry extends printable {      private static final long serialversionuid = -694282168028218725l;      @xmlanyelement     private list<jaxbelement<double>> entries = new arraylist<>();      public list<jaxbelement<double>> getentries() {         return entries;     }      public jaxbexchangeratesentry setentries(list<jaxbelement<double>> entries) {         this.entries = entries;         return this;     } } 

with got following result:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> <exchangerate>     <rates>         <usd>             <usd>9.0</usd>             <eur>7.0</eur>             <gbp>8.0</gbp>         </usd>         <eur>             <usd>3.0</usd>             <eur>1.0</eur>             <gbp>2.0</gbp>         </eur>         <gbp>             <usd>6.0</usd>             <eur>4.0</eur>             <gbp>5.0</gbp>         </gbp>     </rates> </exchangerate> 

how can remove/skip rates tag?

i recommend structure xml like:

<exchangerate>     <date>2015-05-04</date>     <currency code="eur">         <rate code="eur">1</rate >         <rate code="gbp">0.73788</rate >         <rate code="usd">1.1152</rate >     </currency>     <currency code="gbp">         <rate code="eur">1.35523</rate >         <rate code="gbp">1</rate >         <rate code="usd">1.51136</rate >     </currency>     <currency code="usd">         <rate code="eur">0.8967</rate >         <rate code="gbp">0.66166</rate >         <rate code="usd">1</rate >     </currency> </exchangerate>  

and have multiple classes:

@xmlaccessortype(xmlaccesstype.field) public class exchangerates {     @xmljavatypeadapter(dateadapter.class)     private date date;      @xmlelement(name="currency")     private list<currency> currencies = new arraylist<>();      .... }  @xmlaccessortype(xmlaccesstype.field) public class currency {     @xmlattribute     private string code;      @xmlelement(name="rate")     private list<rate> rates= new arraylist<>();      .... }   @xmlaccessortype(xmlaccesstype.field) public class rate {     @xmlattribute     private string code;      @xmlvalue     private double value;      .... } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -