简体   繁体   中英

Getting error while parsing XML to JAVA object using JAXB

I have the following xml file:

<?xml version="1.0" standalone="yes"?>
<interestRateCurve>
  <effectiveasof>2016-04-27</effectiveasof>
  <currency>JPY</currency>
  <baddayconvention>M</baddayconvention>

  <deposits>
    <daycountconvention>ACT/360</daycountconvention>
    <snaptime>2016-04-26T07:00:00.000Z</snaptime>
    <spotdate>2016-04-29</spotdate>

    <calendars>
      <calendar>TYO</calendar>
    </calendars>

    <curvepoint>
      <tenor>1M</tenor>
      <maturitydate>2016-05-30</maturitydate>
      <parrate>-0.00066</parrate>
    </curvepoint>

    <curvepoint>
      <tenor>2M</tenor>
      <maturitydate>2016-06-29</maturitydate>
      <parrate>-0.00042</parrate>
    </curvepoint>

    <curvepoint>
      <tenor>3M</tenor>
      <maturitydate>2016-07-29</maturitydate>
      <parrate>-0.000386</parrate>
    </curvepoint>

    <curvepoint>
      <tenor>6M</tenor>
      <maturitydate>2016-10-31</maturitydate>
      <parrate>-0.000114</parrate>
    </curvepoint>

    <curvepoint>
      <tenor>1Y</tenor>
      <maturitydate>2017-04-28</maturitydate>
      <parrate>0.000758</parrate>
    </curvepoint>

  </deposits>

  <swaps>
    <fixeddaycountconvention>ACT/365</fixeddaycountconvention>
    <floatingdaycountconvention>ACT/360</floatingdaycountconvention>
    <fixedpaymentfrequency>6M</fixedpaymentfrequency>
    <floatingpaymentfrequency>6M</floatingpaymentfrequency>
    <snaptime>2016-04-26T07:00:00.000Z</snaptime>
    <spotdate>2016-04-29</spotdate>

    <calendars>
      <calendar>TYO</calendar>
    </calendars>

    <curvepoint>
      <tenor>2Y</tenor>
      <maturitydate>2018-04-29</maturitydate>
      <parrate>-0.0016</parrate>
    </curvepoint>

    <curvepoint>
      <tenor>3Y</tenor>
      <maturitydate>2019-04-29</maturitydate>
      <parrate>-0.001663</parrate>
    </curvepoint>

    <curvepoint>
      <tenor>4Y</tenor>
      <maturitydate>2020-04-29</maturitydate>
      <parrate>-0.001388</parrate>
    </curvepoint>
    <curvepoint>
      <tenor>5Y</tenor>
      <maturitydate>2021-04-29</maturitydate>
      <parrate>-0.000963</parrate>
    </curvepoint>
    <curvepoint>
      <tenor>6Y</tenor>
      <maturitydate>2022-04-29</maturitydate>
      <parrate>-0.000475</parrate>
    </curvepoint>
    <curvepoint>
      <tenor>7Y</tenor>
      <maturitydate>2023-04-29</maturitydate>
      <parrate>-0.0000063</parrate>
    </curvepoint>

    <curvepoint>
      <tenor>8Y</tenor>
      <maturitydate>2024-04-29</maturitydate>
      <parrate>0.000425</parrate>
    </curvepoint>
    <curvepoint>
      <tenor>9Y</tenor>
      <maturitydate>2025-04-29</maturitydate>
      <parrate>0.000813</parrate>
    </curvepoint>
    <curvepoint>
       <tenor>10Y</tenor>
       <maturitydate>2026-04-29</maturitydate>
       <parrate>0.0012</parrate>
    </curvepoint>

    <curvepoint>
      <tenor>12Y</tenor>
      <maturitydate>2028-04-29</maturitydate>
      <parrate>0.002038</parrate>
    </curvepoint>

    <curvepoint>
      <tenor>15Y</tenor>
      <maturitydate>2031-04-29</maturitydate>
      <parrate>0.00315</parrate>
    </curvepoint>
    <curvepoint>
      <tenor>20Y</tenor>
      <maturitydate>2036-04-29</maturitydate>
      <parrate>0.004588</parrate>
    </curvepoint>

    <curvepoint>
      <tenor>30Y</tenor>
      <maturitydate>2046-04-29</maturitydate>
      <parrate>0.005688</parrate>
    </curvepoint>
  </swaps>
</interestRateCurve>

I'm trying to parse this into a Java object using JAXB unmarshaller. My classes are:

InterestRateCurves.java:

    package IRCurves;

    import java.util.List;  

    import javax.xml.bind.annotation.XmlAttribute;  
    import javax.xml.bind.annotation.XmlElement;  
    import javax.xml.bind.annotation.XmlRootElement;  

    @XmlRootElement  
    public class InterestRateCurves { 
    private String effectiveasof;  
    private String currency;  
    private String baddayconvention;
    private List<Deposits> deposits;  
    private List<Swaps> swaps; 
    public InterestRateCurves() {}  
    public InterestRateCurves(String effectiveasof, String currency, String baddayconvention, List<Deposits> deposits, List<Swaps> swaps) {  
        super();  
        this.effectiveasof = effectiveasof;  
        this.currency = currency;  
        this.baddayconvention = baddayconvention;
        this.deposits = deposits; 
        this.swaps = swaps;
    }  
    @XmlAttribute  
    public String getEffectiveasof() {  
        return effectiveasof;  
    }  
    public void setEffectiveasof(String effectiveasof) {  
        this.effectiveasof = effectiveasof;  
    }  
    @XmlElement  
    public String getCurrency() {  
        return currency;  
    }  
    public void setCurrency(String currency) {  
        this.currency = currency;  
    } 

    @XmlElement  
    public String getBaddayconvention() {  
        return baddayconvention;  
    }  
    public void setBaddayconvention(String baddayconvention) {  
        this.baddayconvention = baddayconvention;  
    } 

    @XmlElement  
    public List<Deposits> getDeposits() {  
        return deposits;  
    }  
    public void setDeposits(List<Deposits> deposits) {  
        this.deposits = deposits;  
    }  

    @XmlElement  
    public List<Swaps> getSwaps() {  
        return swaps;  
    }  
    public void setSwaps(List<Swaps> swaps) {  
        this.swaps = swaps;  
    } 


    }

Deposits.java:

package IRCurves;

import java.util.List;

import javax.xml.bind.annotation.XmlElement;


    public class Deposits {  
        private String daycountconvention;  
        private String snaptime;  
        private String spotdate; 
        private List<Calenders> calenders;
        private List<Curvepoint> curvepoint;

        public Deposits() {}  

        public Deposits(String daycountconvention, String snaptime, String spotdate, List<Calenders> calenders, List<Curvepoint> curvepoint) {  
            super();  
            this.daycountconvention = daycountconvention;  
            this.snaptime = snaptime;  
            this.spotdate = spotdate;
            this.calenders = calenders;
            this.curvepoint = curvepoint;
        }  
        public String getDaycountconvention() {  
            return daycountconvention;  
        }  
        public void setDaycountconvention(String daycountconvention) {  
            this.daycountconvention = daycountconvention;  
        }  
        public String getSnaptime() {  
            return snaptime;  
        }  
        public void setSnaptime(String snaptime) {  
            this.snaptime = snaptime;  
        }  
        public String getSpotdate() {  
            return spotdate;  
        }  
        public void setSpotdate(String spotdate) {  
            this.spotdate = spotdate;  
        }  

        @XmlElement  
        public List<Calenders> getCalenders() {  
            return calenders;  
        }  
        public void setCalenders(List<Calenders> calenders) {  
            this.calenders = calenders;  
        }

        @XmlElement  
        public List<Curvepoint> getCurvepoint() {  
            return curvepoint;  
        }  
        public void setCurvepoint(List<Curvepoint> curvepoint) {  
            this.curvepoint = curvepoint;  
        }

        } 

And similar other POJO classes. My XMLToObject.java is:

package IRCurves;

    import java.io.File;  
    import java.util.List;  

    import javax.xml.bind.JAXBContext;  
    import javax.xml.bind.JAXBException;  
    import javax.xml.bind.Unmarshaller;  

    public class XmlToObject {  
        public static void main(String[] args) {  

         try {  

            File file = new File("InterestRates.xml");  
            JAXBContext jaxbContext = JAXBContext.newInstance(InterestRateCurves.class);  

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();  
            InterestRateCurves que= (InterestRateCurves) jaxbUnmarshaller.unmarshal(file);  

            System.out.println(que.getEffectiveasof()+" "+que.getCurrency()+" "+que.getBaddayconvention());  
            System.out.println("Deposits:");  
            List<Deposits> list=que.getDeposits();  
            for(Deposits ans:list)  
              {
                System.out.println(ans.getDaycountconvention()+" "+ans.getSnaptime()+"  "+ans.getSpotdate());
                System.out.println("Calenders:");  
                List<Calenders> list1=ans.getCalenders();  
                for(Calenders cal:list1)  
                    System.out.println(cal.getCalender());
                System.out.println("Curvepoint:");  
                List<Curvepoint> list2=ans.getCurvepoint();  
                for(Curvepoint curve:list2)  
                    System.out.println(curve.getTenor()+" "+curve.getMaturitydate()+" "+curve.getParrate());

              }

            System.out.println("Swaps:");  
            List<Swaps> list3=que.getSwaps();  
            for(Swaps swap:list3)  
              {
                System.out.println(swap.getFixeddaycountconvention()+" "+swap.getFloatingdaycountconvention()+"  "+swap.getFixedpaymentfrequency()+" "+swap.getFloatingpaymentfrequency()+" "+swap.getSnaptime()+" "+swap.getSpotdate());
                System.out.println("Calenders:");  
                List<Calenders> list1=swap.getCalenders();  
                for(Calenders cal:list1)  
                    System.out.println(cal.getCalender());
                System.out.println("Curvepoint:");  
                List<Curvepoint> list2=swap.getCurvepoint();  
                for(Curvepoint curve:list2)  
                    System.out.println(curve.getTenor()+" "+curve.getMaturitydate()+" "+curve.getParrate());

              }

          } catch (JAXBException e) {  
            e.printStackTrace();  
          }  

        }  
    }

But I'm getting the following error:

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"interestRateCurve"). Expected elements are <{}interestRateCurves>
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:647)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:243)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:238)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:105)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1048)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:483)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:465)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:135)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:378)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:604)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3122)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:880)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:649)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:203)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:175)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:162)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:171)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:189)
    at IRCurves.XmlToObject.main(XmlToObject.java:19)

Please tell me what it means?

Have a look to the answer of @Blaise Douglas for this question , it explains the reason and how to fix it.

the {} portion refers to that qualified name not having the namespace URI portion set.

This answer is for those using XSD Schema. XJC can, and often does, create a package-info.java file to go with the binding code that you generate.

Say your namespace was "data." XJC will put your binding code in the folder ./data . If you leave this file in the folder, you can end up getting an error: unexpected element (uri:"", local:"[nameOfRootElementTag]"). Expected elements are <{data}[nameOfRootElementTag]> unexpected element (uri:"", local:"[nameOfRootElementTag]"). Expected elements are <{data}[nameOfRootElementTag]>

In the package-info.java file you'll see the following:

@javax.xml.bind.annotation.XmlSchema
(elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package data;

Remove the arguments from the @XMLSchema notation, and the error should go away.

@javax.xml.bind.annotation.XmlSchema
package data;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM