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的相关知识--工厂. 工厂允许你动态的创建测试.例如,假设 ...
随机推荐
- 用python的numpy作线性拟合、多项式拟合、对数拟合
转自:http://blog.itpub.net/12199764/viewspace-1743145/ 项目中有涉及趋势预测的工作,整理一下这3种拟合方法:1.线性拟合-使用mathimport m ...
- Linux Shell 数学运算
Linux Shell 数学运算 在Linux中直接使用数学运算符进行数学运算往往得不到我们想要的计算结果.要在Shell中进行数学运算,我们需要借助点小手段.目前,Linux Shell中进行数学运 ...
- 【noip2009】靶形数独
题解: 又是搜索- - 加状态压缩剪枝 二进制记下每行 每列 每个九宫格用过的数是谁 枚举的时候可以O(1)判断冲突 还有个很重要的剪枝 把可能使用数字最少的格子先搜索 代码: #include &l ...
- 【组队训练】2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest
好多oj都崩掉了,于是打了cf.. 开始开的最后一题...尼玛题好长终于看完了...神题不会.... I过了好多人..看了下,一眼题...随便敲了下,1A ]; int main(){ int n, ...
- 问题-Delphi 中使用TStringList后,报out of memory 的解决方法
问题现象: 请一段开发个项目 程序调试全部通过但测试时出现个问题 “out of memory” 在长时间运行时!后来终于解决 :很简单其实就是object.create时对象没有释放. 代码如下: ...
- 1000万条数据导入mysql
今天需要将一个含有1000万条数据的文本内容插入到数据库表中,最初自然想到的是使用Insertinto '表名'values(),(),()...这种插入方式,但是发现这种方式对1000万条数据量的情 ...
- IEnumerable和IEnumerator 详解
初学C#的时候,老是被IEnumerable.IEnumerator.ICollection等这样的接口弄的糊里糊涂,我觉得有必要切底的弄清楚IEnumerable和IEnumerator的本质. 下 ...
- ECSHOP在线手册布局参考图--商品分类页 category.dwt
A.购物车 1,设置方法 程序自动读取购物车的商品数量 2,代码相关 cart.lbi 中 {insert_scripts files='transport.js'} <div clas ...
- Central Authentication Service
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- Http协议网络对时工具
2009-09-17 11:52:31 专业的网络对时软件大多採用NTP协议来获取专业时间server时间对时,也有採用SNTP协议的,本来也想做个SNTP或NTP协议的对时工具自己用,尽管我手上也有 ...