是否可以配置JAXB上下文(用于unmarshaller)本身,以便它可以实例化具有访问器getField()/ setField()和字段的类A的上下文? A类不可修改:它是第三方或生成。
class A {
public int field;
public int getField();
public void setField(int field);
}
标准JAXBContext实例化
JAXBContext jaxbContext = JAXBContext.newInstance(A.class);
会给出以下异常
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Class has two properties of the same name "field"
this problem is related to the following location:
at public int com.thecompany.A.getField()
at com.thecompany.A
this problem is related to the following location:
at public int com.thecompany.A.field
at com.thecompany.A
可以通过使用
注释类来解决@XmlAccessorType(XmlAccessType.FIELD)
但这不是可接受的解决方案。
是否可以保持类定义不受影响,并且能够从XML文件中读取其实例? (可能不是JAXB?)
UPD:发现同样的问题已经回答:JAX-B - How to map a schema element to an existing Java class
或者,在此答案中使用http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted:Creating Jaxb classes from xml without schema;很好的教程在http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html
给出答案 0 :(得分:0)
JAXB外部绑定允许您在不更改来源的情况下进行编组/解组,我相信这是您所要求的。
http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/2.0/tutorial/doc/JAXBUsing4.html