我有以下xml文件:
<?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>
我试图使用JAXB unmarshaller将其解析为Java对象。我的课程是:
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;
}
}
和其他类似的POJO课程一样。我的XMLToObject.java是:
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();
}
}
}
但我收到以下错误:
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)
请告诉我这意味着什么?
答案 0 :(得分:0)
查看@Blaise Douglas对this question的回答,它解释了原因以及解决方法。
{}部分指的是没有设置名称空间URI部分的限定名称。
答案 1 :(得分:0)
此答案适用于使用XSD架构的人员。 XJC可以并且经常创建一个package-info.java文件,以与您生成的绑定代码一起使用。
说您的命名空间是“数据”。 XJC会将您的绑定代码放在文件夹./data
中。如果将此文件保留在文件夹中,可能会导致错误:unexpected element (uri:"", local:"[nameOfRootElementTag]"). Expected elements are <{data}[nameOfRootElementTag]>
在package-info.java文件中,您将看到以下内容:
@javax.xml.bind.annotation.XmlSchema
(elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package data;
从@XMLSchema
标记中删除参数,错误应该消失。
@javax.xml.bind.annotation.XmlSchema
package data;