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

JAXB 2的ObjectFactory类有什么意义?

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

JAXB 2的ObjectFactory类有什么意义?

向后兼容性不是唯一原因。:-P

对于更复杂的模式,例如对元素内容可以采用的值具有复杂约束的模式,有时您需要创建实际

JAXBElement
对象。通常,手工创建它们并不容易,因此这些
create*
方法为您完成了艰巨的工作。示例(来自XHTML
1.1模式):

@XmlElementDecl(namespace = "http://www.w3.org/1999/xhtml", name = "style", scope = XhtmlHeadType.class)public JAXBElement<XhtmlStyleType> createXhtmlHeadTypeStyle(XhtmlStyleType value) {    return new JAXBElement<XhtmlStyleType>(_XhtmlHeadTypeStyle_QNAME, XhtmlStyleType.class, XhtmlHeadType.class, value);}

这是将

<style>
标签放入
<head>
标签的方法:

ObjectFactory factory = new ObjectFactory();XhtmlHtmlType html = factory.createXhtmlHtmlType();XhtmlHeadType head = factory.createXhtmlHeadType();html.setHead(head);XhtmlStyleType style = factory.createXhtmlStyleType();head.getContent().add(factory.createXhtmlHeadTypeStyle(style));

ObjectFactory
可以认为前三个用途是多余的(尽管对于一致性很有用),但是第四个用途使JAXB变得非常容易使用。成像
newJAXBElement
每次都要手工写出来!



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

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

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