2017-05-29 74 views
0

给定一个XML及相关产生 XSD文件,我在解编与JAXB的XML问题。JAXB:缺少“XSI型”属性在解组XML

考虑以下代码:

JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class); 
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 
Envelope envelope = ((JAXBElement<Envelope>) jaxbUnmarshaller.unmarshal(file)).getValue(); 

XML文件是正确的解组,但我失去了对每一个信息 “的xsi:type” 属性。

在生成的类中,没有任何可以匹配该信息的“类型”字段。

回答

0

的解决方案是增加的代码这两条线在生成的类:

@XmlAttribute(name = "type", namespace = "http://www.w3.org/2001/XMLSchema-instance") 
protected java.lang.String type; 

还要添加getter/setter方法。

以这种方式,正确读取“xsi:type”属性。