0
<?xml version="1.0" encoding="UTF-8"?>
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="2" yahoo:created="2016-02-17T21:25:49Z" yahoo:lang="en-US">
   <results>
      <rate id="USDEUR">
         <Name>USD/EUR</Name>
         <Rate>0.8990</Rate>
         <Date>2/17/2016</Date>
         <Time>9:26pm</Time>
         <Ask>0.8991</Ask>
         <Bid>0.8990</Bid>
      </rate>
      <rate id="USDUAH">
         <Name>USD/UAH</Name>
         <Rate>26.9000</Rate>
         <Date>2/17/2016</Date>
         <Time>9:25pm</Time>
         <Ask>27.0000</Ask>
         <Bid>26.9000</Bid>
      </rate>
   </results>
</query>

I trying to parse this document by this code:

@XmlRootElement(name="query")
@XmlAccessorType(XmlAccessType.FIELD)
public class Query {

   public List<Rate> rate = new ArrayList<>();

    @XmlElement( name = "rate" )
    public void setQuery( List rate )
    {
        this.rate = rate;
    }
       @Override
  public String toString() {
    return "Query{" +
            "rate=" + rate +
            '}';
}
}

/////////////////////////////////////////////////////////////////////////




 @XmlRootElement( name = "rate" )
    public class Rate {
        String id;

        @XmlElement( name = "id" )
        public void setId(String id){
            this.id = id;
        }

        String rate;

        @XmlElement( name = "rate" )
        public void setRate(String rate){
            this.rate = rate;
        }
    }
//////////////////////////////////////////////

File file = new File("E://xml//2.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Query.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Query mu = (Query) jaxbUnmarshaller.unmarshal(file);
System.out.println(mu);

What's wrong with it? thrice rewriting classes , stopped on this variant

In console : Query{rate=[]}

ArrayList Rate isn't associated with nodes from the document //dummy text

Xstian
  • 8,184
  • 10
  • 42
  • 72
user202822
  • 99
  • 1
  • 1
  • 9

1 Answers1

0

Your classes were wrong.

Query.java

@XmlRootElement(name="query")
@XmlAccessorType(XmlAccessType.FIELD)
public class Query {

    @XmlElementWrapper
    @XmlElement(name="rate")
    private List<Rate> results;

    public List<Rate> getRate() {
        return results;
    }

    public void setRate(List<Rate> rate) {
        this.results = rate;
    }
}

Rate.java

@XmlRootElement( name = "rate" )
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "rate", propOrder = {
        "rate"
    })
public class Rate {

    @XmlAttribute(name="id")
    private String id;

    @XmlElement(name="Rate")
    private String rate;

    public String getId() {
        return id;
    }

    public void setId(String id){
        this.id = id;
    }

    public String getRate() {
        return rate;
    }

    public void setRate(String rate) {
        this.rate = rate;
    }


}
Xstian
  • 8,184
  • 10
  • 42
  • 72
  • Thank you. What does that annotation mean? Programm works without it. @XmlType(name = "rate", propOrder = { "rate" } – user202822 Feb 18 '16 at 09:29
  • [See here another stackoverflow answer](http://stackoverflow.com/questions/11520724/xmlrootelement-versus-xmltype) Anyway feel free to up to vote. – Xstian Feb 18 '16 at 09:53