JAXB - The Object Factory
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();
A 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的更多相关文章
- 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 ...
- 问题-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 ...
- Table of Contents - JAXB
Getting Started Hello World Hello World with Namespace xjc - 将 XML Schema 编译成 Java 类 wsimport: 编译 WS ...
- JAXB - Hello World
We'll stick with the tradition and use a sort of "Hello World" XML document to illustrate ...
- JAXB - Calling marshal
Only a handful of source code lines is required to make a JAXB Marshaller object write a document tr ...
- 4.工厂方法模式(Factory Method)
耦合关系: 动机(Motivation): 在软件系统中,由于需求的变化,"这个对象的具体实现"经常面临着剧烈的变化,但它却有比较稳定的接口. 如何应对这种 ...
- C# 设计模式-工厂模式(Factory)
1.工厂模式 factory从若干个可能类创建对象. 例如:如果创建一个通信类接口,并有多种实现方式,可以使用factory创建一个实现该接口的对象,factory可以根据我们的选择,来创建适合的对象 ...
- TestNG中@Factory的用法一:简单的数据驱动
为什么要使用@Factory注解呢,先来看下面这个例子 被测试类Person package ngtest; import org.testng.annotations.Parameters; imp ...
- Java自动化测试框架-07 - TestNG之Factory篇 - 欢快畅游梦幻工厂(详细教程)
简介 最近忙着装修博客园,没时间更新文章,今天终于抽出时间把上次写的一半的文章给写完了,新的博客园风格,希望大家喜欢.今天继续介绍testng的相关知识--工厂. 工厂允许你动态的创建测试.例如,假设 ...
随机推荐
- 挑战树莓派:谁才是Geek最爱的开发板?
树莓派(Raspberry Pi)是一块跟信用卡差不多大小的开发板,它的初衷是以低廉的硬件和开源软件扶持一些落后地区的电脑科学教育.由于它在性能和价格方面有一个很好的平衡点,所以很多硬件玩家也想买一个 ...
- Java中4大基本加密算法解析
简单的java加密算法有: BASE64 严格地说,属于编码格式,而非加密算法 MD5(Message Digest algorithm 5,信息摘要算法) SHA(Secure Hash Algor ...
- unigui下载文件
lblProduct.Caption := '<a href="files\1.t" target=new>要下载的文件名</a>';
- MapReduce的流程
1. Inputformat会从job的INPUT_DIR目录下读入待处理的文件,检查输入的有效性并将文件切分成InputSplit列表.Job实例可以通过setInputFormatClass(Cl ...
- 怎样在Java中实现基本数据类型与字符之间的转换
摘要:在我们对Java的学习当中数据类型之间的转换,是我们常见的事,我们也都知道基本数据类型之间有自动转换和强制转换,在int . short . long .float .double之间的转 ...
- iOS动画详解(一)
Core Graphics Framework是一套基于C的API框架,使用了Quartz作为绘图引擎.它提供了低级别.轻量级.高保真度的2D渲染.该框架可以用于基于路径的绘图.变换.颜色管理.脱屏渲 ...
- 获取Application中的spring容器
方式1: ApplicationContext ac = (ApplicationContext) invocation.getInvocationContext().getApplication() ...
- android studio 真机调试
1.使用usb连接电脑,打开开发者选项,打开usb调试. 2.不能使用仅充电,修改为媒体设备(MTP) 3.如果无法连接可以使用豌豆荚或者金山手机助手连接. 4.运行android程序即可. 5.如果 ...
- getJSON的用法
一.方法定义:jQuery.getJSON( url, data, callback ) 通过get请求得到json数据 ·url用于提供json数据的地址页 ·data(Optional)用于传送到 ...
- Android develop tricks——整理自国外的一些Blog
ViewDragHelper --视图拖动是一个比較复杂的问题.这个类能够帮助解决不少问题.假设你须要一个样例,DrawerLayout就是利用它实现扫滑.Flavient Laurent 还写了一些 ...