JAXB unmarshaller.unmarshal何时返回JAXBElement 或MySchemaObject?

JAXB unmarshaller.unmarshal何时返回JAXBElement 或MySchemaObject?,第1张

JAXB unmarshaller.unmarshal何时返回JAXBElement 或MySchemaObject?

如果根元素唯一地对应于Java类,则将返回该类的实例,否则将返回a

JAXBElement

如果要确保始终获取域对象的实例,则可以利用

JAXBInstrospector
。下面是一个例子

演示版

package forum10243679;import java.io.StringReader;import javax.xml.bind.*;import javax.xml.transform.stream.StreamSource;public class Demo {    private static final String XML = "<root/>";    public static void main(String[] args) throws Exception {        JAXBContext jc = JAXBContext.newInstance(Root.class);        Unmarshaller unmarshaller = jc.createUnmarshaller();        JAXBIntrospector jaxbIntrospector = jc.createJAXBIntrospector();        Object object = unmarshaller.unmarshal(new StringReader(XML));        System.out.println(object.getClass());        System.out.println(jaxbIntrospector.getValue(object).getClass());        Object jaxbElement = unmarshaller.unmarshal(new StreamSource(new StringReader(XML)), Root.class);        System.out.println(jaxbElement.getClass());        System.out.println(jaxbIntrospector.getValue(jaxbElement).getClass());    }}

输出量

class forum10243679.Rootclass forum10243679.Rootclass javax.xml.bind.JAXBElementclass forum10243679.Root


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5616068.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存