栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/609935.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号