`
george.gu
  • 浏览: 73514 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

OXM: JAXB2.0 in JDK1.6

阅读更多

 

1.1.1 JAXB 2.0: ObjectàXML

Using JAXB 2.0 embedded in JDK 1.6.

Marshal Object to XML bytes:

      public byte[] marshalToBytes(Object o) throws Exception {

            JAXBContext context = JAXBContext.newInstance(o.getClass());

 

            ByteArrayOutputStream bos = new ByteArrayOutputStream();

            Marshaller m = context.createMarshaller();

            m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); //set XML encoding.

            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); //output formatted xml.

            m.setProperty(Marshaller.JAXB_FRAGMENT, false);// ignore XML header or not.

            m.marshal(o, bos);

 

            return bos.toByteArray();

      }

If you want to output XML to specific file, please udpate "ByteArrayOutputStream bos" to write XML into external file.

1.1.2 Object class should have empty constructor

 

1.1.3 @XmlTransient to ignore some properties

Some temporary data or some cycle reference relation.

 

For example, DDFNode.parentDDFNode.

 

1.1.4 @XmlJavaTypeAdapter

@XmlJavaTypeAdapter is used to adapter complex Object which is not recognized by JAXB into a simple or customized Object.

1) XmlAdapter.marshal(BoundType):

a. Convert Object to another ValueType which is recognized by JAXB. Or Which is customized rather than JAXB.

b. Used when ObjectàXml

2) XmlAdapter.unmarshal(ValueType):

a. Convert ValueType Object to java Object requested by business logical.

b. Used when XmlàObject

 

“List<String> tacs” instead of “Set tacs”

 

@XmlJavaTypeAdapter on field will lead to additional container tag. For example:

@XmlJavaTypeAdapter(FileNamesAdapter.class)

Map<String, String> profileMappings;

Will lead to XML like following:

           

            < profileMappings >

                        <profile-mapping …/>

                        <!--your wrapper xml element-->;

            </ profileMappings >

But not:

            <profile-mapping …/>

 

It’s better to use List<> as collection implementation. And all the Object use simple type.

 

There should be dedicated Xml Java adapter for DDFNode, because:

1) DFPropterties is dedicated complex element

2) Some elements have empty tag

3) List of child Nodes

4) …

 

1.1.5 @XmlType: Order of the Element:  @XmlType(propOrder = {“parameterNames”})

Use field name but not element name.

All the fields should be specified, even some of them are ignored by @XmlTransient.

1.1.6 Do not set CYCLE reference on Objects.

Normally, JAXB can detect the cycle reference problem.

1.1.7 @XmlSeeAlso: used to bind related InteriorDDFNode and LeafDDFNode

@XmlSeeAlso({InteriorDDFNode.class, LeafDDFNode.class})

public abstract class DDFNode extends IdClass {

           

}

@XmlSeeAlso can be used to link the bund to its related subclasses. It will be used to define InteriorDDFNode and LeafDDFNode. Because DDFTree use following definition:

    @XmlElement(name = "Node")

    private List<DDFNode> ddfNodes;

 

 

1.1.8 @XmlElementWrapper or not

@XmlElementWrapper is used to add a container tag for list,

For model/ddf, model/profile-mapping, there is no @XmlElementWrapper.

But for ddf*.xml, @XmlElementWrapper is used to define container, such as Node/DFProperties/AccessType(Get?, Add?, …).

1.1.9 @XmlSeeAlso + @XmlJavaTypeAdapter = what will happen????

 

1.1.10 @XmlElements + @XmlJavaTypeAdapter to generate DDFNode/DFProperties

There is no dedicated DFProperties bean object inside DDFNode.java.

Information for DFProperties are dynamic and has different value types.

 

Use XmlJavaTypeAdapter convert String value list to AccessType Object List:

                        List<AccessTypeBase> accessTypes = new ArrayList<AccessTypeBase>();

                        for (String access : b.accessTypes) {

                                    if (access.equalsIgnoreCase(AccessTypeAdd.ACCESS)) {

                                                accessTypes.add(new AccessTypeAdd());

                                    } else if (access.equalsIgnoreCase(AccessTypeCopy.ACCESS)) {

                                                accessTypes.add(new AccessTypeCopy());

                                    } else if (access.equalsIgnoreCase(AccessTypeDelete.ACCESS)) {

                                                accessTypes.add(new AccessTypeDelete());

                                    } else if (access.equalsIgnoreCase(AccessTypeExec.ACCESS)) {

                                                accessTypes.add(new AccessTypeExec());

                                    } else if (access.equalsIgnoreCase(AccessTypeReplace.ACCESS)) {

                                                accessTypes.add(new AccessTypeReplace());

                                    } else {

                                                accessTypes.add(new AccessTypeGet());

                                    }

                        }

 We can also use Map<accessTyeString, classNameString> to ignore many if...else....

 

Define XmlElements according to different Access Type class instance:

            @XmlElementWrapper(name = "AccessType")

            @XmlElements( {

                        @XmlElement(name="Add",type=AccessTypeAdd.class),

                        @XmlElement(name="Copy",type=AccessTypeCopy.class),

                        @XmlElement(name="Delete",type=AccessTypeDelete.class),

                        @XmlElement(name="Exec",type=AccessTypeExec.class),

                       @XmlElement(name="Get",type=AccessTypeGet.class),

                       @XmlElement(name="Replace",type=AccessTypeReplace.class)

            })

            public List<AccessTypeBase> accessTypes;

 

Then it will generate the XML format like following:

        <AccessType>

            <Get/>

            <Add/>

            <Replace/>

        </AccessType>

 

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    spring-oxm:Spring3 JAXB编组和反编组

    春毒Spring3 JAXB编组和反编组。 #先决条件:-您应该拥有jdk1.7.x,apache tomcat7.x,apache-maven-3.2.3-bin和Git-1.9.4客户端工具。 #step1:-从回购中克隆项目。 #step2:-运行项目中已经存在的pom.xml Maven...

    Java JDK1.5、1.6、1.7新特性整理

    2. **JAXB2**:JAXB 提供了对象到 XML 的映射功能,实现了 OXM(Object XML Mapping),使得对象和 XML 文档之间的转换更加方便。 ### Java JDK 1.7 新特性 尽管没有直接给出 JDK 1.7 的特性,但这里可以列出一些...

    javaweb知识点整理.docx

    现在我们可以用 JDK1.6 的 Compiler API(JSR 199)去动态编译 Java 源文件,Compiler API 结合反射功能就可以实现动态的产生 Java 代码并编译执行这些代码,有点动态语言的特征。这个特性对于某些需要用到动态编译...

    发布webService服务接口与spring整合教程

    &lt;bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"&gt; &lt;value&gt;com.yourcompany.yourproject.YourRequestType&lt;/value&gt; &lt;value&gt;com.yourcompany.yourproject.YourResponseType&lt;/value&gt;...

    spring使用OXM进行对象XML映射解析

    "春使用OXM进行对象XML映射解析" 春框架中使用OXM(Object-XML Mapping)对对象进行XML映射解析,具有一定借鉴价值。下面将详细介绍春使用OXM进行对象XML映射解析的技术要点。 1. 认识XML解析技术 XML...

    spring和hibernate__jar包,详细说明看jar包列表

    hibernate-jpa-2.0-api-1.0.1.Final.jar hibernate-validator-4.0.2.GA.jar httpclient-4.1.2.jar httpcore-4.1.2.jar jackson-core-asl-1.7.2.jar jackson-core-asl-1.9.9.jar jackson-jaxrs-1.7.2.jar jackson-jax...

    (转载)自己动手写一个spring

    5. **数据访问/集成**: 包括JDBC、ORM(Object-Relational Mapping,如Hibernate)、OXM(Object-XML Mapping,如JAXB)等支持,方便数据库和XML的交互。 6. **Web模块**: 提供了MVC框架、WebSocket支持、Servlet...

    spring源码 spring

    6. **数据访问/集成**:Spring支持多种数据访问技术,包括JDBC、ORM(Object-Relational Mapping,如Hibernate、JPA)和OXM(Object-XML Mapping,如JAXB)。 7. **MVC框架**:Spring MVC是Spring为构建Web应用程序...

    Spring基础知识详解

    6. **数据访问集成**:Spring提供了对各种数据库访问技术的支持,包括JDBC、ORM(Object-Relational Mapping,如Hibernate、MyBatis)和OXM(Object-XML Mapping,如JAXB)。Spring JDBC提供了一套模板方法来简化...

    spring3 中的各种jar文件说明

    它与spring-aspects.jar配合,支持JDK动态代理和AspectJ的编译时及运行时织入。 5. **spring-expression.jar (SpEL)**:Spring表达式语言,提供强大且灵活的在运行时查询和操作对象图的能力。它用于在配置文件中...

    Spring 相关jar包简介 (spring 的包)

    12. **spring-oxm.jar**:对象/XML映射模块,提供了对不同XML绑定技术如JAXB、Castor、XStream的集成。 13. **spring-instrument.jar**:提供了类加载器的工具和接口,主要用于服务器端的应用,如Tomcat,提升类的...

    Spring 全部 jar包

    11. **spring-oxm.jar**:提供了对象到XML映射的支持,支持如JAXB、Castor、XMLBeans和XStream等库,用于对象的序列化和反序列化。 在开发基于Spring的应用程序时,通常根据项目需求选择上述jar包的子集。这些基础...

    Spring.3.x企业应用开发实战(完整版).part2

    1.6 Spring对Java版本的要求 1.7 如何获取Spring 1.8 小结 第2章 快速入门 2.1 实例功能概述 2.1.1 比Hello World更适用的实例 2.1.2 实例功能简介 2.2 环境准备 2.2.1 创建库表 2.2.2 建立工程 2.2.3 类包及Spring...

    Spring3.x企业应用开发实战(完整版) part1

    1.6 Spring对Java版本的要求 1.7 如何获取Spring 1.8 小结 第2章 快速入门 2.1 实例功能概述 2.1.1 比Hello World更适用的实例 2.1.2 实例功能简介 2.2 环境准备 2.2.1 创建库表 2.2.2 建立工程 2.2.3 类包及Spring...

    简介Java的Spring框架的体系结构以及安装配置

    - **OXM模块**:提供对象/XML映射的抽象层,支持JAXB、Castor、XMLBeans、JiBX和XStream等。 - **JMS模块**:用于处理Java消息服务。 - **事务模块**:支持编程和声明式的事务管理。 3. **Web层**: - **Web...

Global site tag (gtag.js) - Google Analytics