我试着在这里查找解决方案,但没有一个能为我工作。因此这个帖子。
以下是我用来调用JAXBContext
的代码 GH_ACCOUNT= nbari:DEFAULT
这是我的示例XML,我正在尝试unMarshall:
final JAXBContext jaxbContext = JAXBContext.newInstance(new Class[] { ClassA.class, ClassB.class });
final Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
return (ClassA) jaxbUnmarshaller.unmarshal(content);
ClassA的:
<?xml version="1.0" encoding="utf-8"?>
<ElementList>
<Element El1="111" EL2="222" EL3="333" EL4="444" />
<Element El1="1111" EL2="22222" EL3="3333" EL4="4444" />
</ElementList
ClassB的:
@XmlRootElement(name = "ElementList")
@XmlAccessorType(XmlAccessType.FIELD)
class ClassA {
@XmlElement(name = "Element")
private List<ClassB> classBList;
ClassA(final List<ClassB> classBList) {
this.classBList = classBList;
}
List<ClassB> getClassBList() {
return classBList;
}
void setClassBList(final List<ClassB> classBList)
{
this.classBList = classBList;
}
}
我正在
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Element")
@XmlType(name = "Element", propOrder = { "El1Value", "EL2Value", "EL3Value", "EL4Value"})
class ClassB {
@XmlAttribute(name = "El1")
private String El1Value;
@XmlAttribute(name = "EL2")
private String EL2Value;
@XmlAttribute(name = "EL3")
private String EL3Value;
@XmlAttribute(name = "EL4")
private String EL4Value;
//Getters and Setters for values
}
即使经过其他解决方案我也得到了这个,所以我确信我错过了一些东西或者做了一些不正确的事情。我花了很多时间在这上面。
有人可以帮我解决这个问题吗?我非常感谢你的帮助。
答案 0 :(得分:2)
我已经测试了您的代码,这是完整的错误:
Exception in thread "main" com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
com.package.ClassA does not have a no-arg default constructor.
this problem is related to the following location:
at com.package.ClassA
解组失败,因为类A缺少默认构造函数,即没有参数的构造函数。添加一个可以解决问题。