Usually hidden in the middle of the list of the classes derived from the types defined in an XML schema there will be one class called ObjectFactory. It's convenient to use the methods of this class because they provide an easy way of creating elements that have to be represented by a JAXBElement<?> object. Given that the top-level element of a document is represented as a JAXBElement<RulebaseType> with the tag "rulebase", one such doument object can be created by code as shown below.

ObjectFactory objFact = new ObjectFactory();
RulebaseType rulebase = objFact.createRulebaseType();
JAXBElement<RulebaseType> doc = objFact.createRulebase( rulebase );

A simple element that does not require a JAXBElement<?> wrapper is created by a straightforward method call.

ModuleType module = objFact.createModuleType();

JAXBElement<?> is also required for element sequences containing elements of the same type but with differing tags. Here is a schema snippet:

<xsd:complexType name="FooBarListType">
<xsd:sequence>
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element name="foo" type="FooBarType"/>
<xsd:element name="bar" type="FooBarType"/>
</xsd:choice>
</xsd:sequence>
</xsd:complexType>

The ObjectFactory would now contain several methods for creating a FooBarListType and its offsprings. A possible sequence of calls is shown in the Java code below.

FooBarListType fblElem = objFact.createFooBarListType();
List<JAXBElement<FooBarType>> fbList = fblElem.getFooOrBar(); // create a "foo" element
FooBarType foo = objFact.createFooBarType();
// ...(add attributes and components to foo)
// Create the element <foo>...</foo>
JAXBElement<FooBarType> fooElem = objFact.createFooBarTypeFoo( foo );
// Add it to its parent's list.
fbList.add( fooElem ); // create a "bar" element
FooBarType bar = objFact.createFooBarType();
// ...(add attributes and components to bar)
// Create the element <bar>...</bar>
JAXBElement<FooBarType> barElem = objFact.createFooBarTypeBar( bar );
// Add it to its parent's list.
fbList.add( barElem );

You may avoid these complications by subtyping FooBarType into identical types FooType and BarType.

JAXB - The Object Factory的更多相关文章

  1. JAXB - Annotations, The Object Factory: XmlRegistry, XmlElementDecl

    To be able to create objects from XML elements, the unmarshaller must have an object factory with me ...

  2. 问题-XE8报Object factory for class{xx-xx-xx-xx-xx} is missing. To register it, you can drop component[TFDGUIxWaitCursor] into your project.

    问题现象:XE8开发数据访问程序时放入了FDPhysMSSQLDriverLink1.FDConnection1.FDConnection1.FDQuery1.DBGrid1,设计期没法,运行期报&q ...

  3. Table of Contents - JAXB

    Getting Started Hello World Hello World with Namespace xjc - 将 XML Schema 编译成 Java 类 wsimport: 编译 WS ...

  4. JAXB - Hello World

    We'll stick with the tradition and use a sort of "Hello World" XML document to illustrate ...

  5. JAXB - Calling marshal

    Only a handful of source code lines is required to make a JAXB Marshaller object write a document tr ...

  6. 4.工厂方法模式(Factory Method)

    耦合关系:       动机(Motivation):    在软件系统中,由于需求的变化,"这个对象的具体实现"经常面临着剧烈的变化,但它却有比较稳定的接口.    如何应对这种 ...

  7. C# 设计模式-工厂模式(Factory)

    1.工厂模式 factory从若干个可能类创建对象. 例如:如果创建一个通信类接口,并有多种实现方式,可以使用factory创建一个实现该接口的对象,factory可以根据我们的选择,来创建适合的对象 ...

  8. TestNG中@Factory的用法一:简单的数据驱动

    为什么要使用@Factory注解呢,先来看下面这个例子 被测试类Person package ngtest; import org.testng.annotations.Parameters; imp ...

  9. Java自动化测试框架-07 - TestNG之Factory篇 - 欢快畅游梦幻工厂(详细教程)

    简介 最近忙着装修博客园,没时间更新文章,今天终于抽出时间把上次写的一半的文章给写完了,新的博客园风格,希望大家喜欢.今天继续介绍testng的相关知识--工厂. 工厂允许你动态的创建测试.例如,假设 ...

随机推荐

  1. [OC Foundation框架 - 5] NSString的常用方法

             NSString *s1 = ";   1.比较 使用 == 号比较的是字符串地址 NSString *s4 = @"abcdefg"; NSStrin ...

  2. 新建数据库,然后使用SQL语句创建表、存储过程、用户说明

    需要在数据库的安全性,用户那里为用户设置一下权限

  3. Cocos2d-x 重写draw方法绘制直线等图形时被遮挡覆盖问题的一种解决方案

    最近在学习过程cocos2dx的过程中需要使用到绘制直线的功能,所以我就采用了引擎中 ccDrawLine 方法,然后重写 draw 方法,在该方法中绘制直线. 但是出现了一个问题,那就是绘制的图形被 ...

  4. Oracle- plsql developer如何查询SQL语句执行历史记录

    相信很多在plsql developer调试oracle的朋友,经常会遇到在plsql developer执行的某一条SQL语句没有保存,那么我们在plsql developer下如何找到我们执行过的 ...

  5. ThindPad x230 无法U盘启动

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  6. 【不积跬步,无以致千里】五个常用的Linux监控脚本代码

    为大家提供五个常用Linux监控脚本(查看主机网卡流量.系统状况监控.监控主机的磁盘空间,当使用空间超过90%就通过发mail来发警告.监控CPU和内存的使用情况.全方位监控主机),有需要的朋友不妨看 ...

  7. HDU 5500 Reorder the Books 贪心

    Reorder the Books Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...

  8. 基于华为Java编程规范的Eclipse checkStyle.xml

    发现项目组成员代码规范存在较大的问题,于是就在华为编程规范的基础上制定了这份checkStyle.xml文档,至于Eclipse怎么安装checkStyle插件以及该插件怎么使用请自行Google之. ...

  9. mysqldump原理3

    现网中数据库运维时,要经常对数据库做热备.为保证恢复时数据的完整性与一致性, 一种方法是在备份之前锁表,但锁表会影响正在运行的业务. mysqldump是当前MySQL中最常用的备份工具,通过mysq ...

  10. string与数值之间的转换

    9.50 编写程序处理一个vector<string>,其元素都表示整数型.计算vector中所有元素之和.修改程序,使之计算表示浮点值的string之和. 程序如下: #include& ...