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();

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.

时间: 2024-10-12 16:31:12

JAXB - The Object Factory的相关文章

JS object factory and inherit sample

/* * Object factory */ function objectFactory(jsonObj){ function objectEntity(){ } if(typeof jsonObj == "object"){ for(var index in jsonObj){ objectEntity.prototype[index] = jsonObj[index]; } } return objectEntity; } var Person = objectFactory({

使用JAXB完成object和xml的转换

1.将 xml 文件中的各个节点和属性信息创建对应的Java模型. 2.在Java模型中的创建与 xml 文件对应的节点和属性需要用注解来表示. @XmlRootElement   将一个Java类映射为一段XML的根节点 参数: name  定义这个根节点的名称 namespace   定义这个根节点命名空间 @XmlAccessorType  定义映射这个类中的何种类型需要映射到XML.可接收四个参数,分别是: XmlAccessType.PROPERTY:映射这个类中的属性(get/set

JAXB - Hello World

We'll stick with the tradition and use a sort of "Hello World" XML document to illustrate the typical scenario for creating the Java classes and their use to marshal a document. We'll not discuss any details in this subsection; it's just here to

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

factory从若干个可能类创建对象. 例如:如果创建一个通信类接口,并有多种实现方式,可以使用factory创建一个实现该接口的对象,factory可以根据我们的选择,来创建适合的对象. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Timers; namespace Demo { public interface ICommunication

Magento 2 Factory Objects

In object oriented programming, a factory method is a method that’s used to instantiate an object. Factory methods exist to ensure system developers have control over how a particular object is instantiated, and how its arguments are passed in. There

JAXB - Calling marshal

Only a handful of source code lines is required to make a JAXB Marshaller object write a document tree as an XML file. First you obtain a Marshaller from a JAXBContext. Then, you might set a number of properties, such as the one that's used below, wh

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

为什么要使用@Factory注解呢,先来看下面这个例子 被测试类Person package ngtest; import org.testng.annotations.Parameters; import org.testng.annotations.Test; /** * @author Administrator * */ public class Person{ String name; int age; @Parameters({"name","age"}

笔记整理

1, Stack, heap, contructor Stack: method invocations, local variables 所以在eclipse里查看stack trace,最上面的是当前调用的方法,当结束当前方法,其就会被移出stack. variable: primitive, or non-primitive:Object 注意local variable如果是reference to object, stack只会存放reference,真正的object还是会在heap

java面试题大全

java面试笔试题大汇总     第一,谈谈final, finally, finalize的区别. 最常被问到. 第二,Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以implements(实现)interface(接口)? 第三,Static Nested Class 和 Inner Class的不同,说得越多越好(面试题有的很笼统). 第四,&和&&的区别. 这个问得很少. 第五,HashMap和Hashtable的区