`
george.gu
  • 浏览: 70974 次
  • 性别: 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...

    spring-oxm-3.2.9.RELEASE.jar

    Spring oxm:Spring对于object/xml映射的支持,可以让JAVA与XML之间来回切换; spring-oxm是操作XML文件:生成,修改节点的完美第三方库

    oxm:oxm是用于* NIX兼容系统的基于源的软件包管理器

    oxm:oxm是用于* NIX兼容系统的基于源的软件包管理器

    spring-oxm-5.3.10-API文档-中文版.zip

    赠送jar包:spring-oxm-5.3.10.jar; 赠送原API文档:spring-oxm-5.3.10-javadoc.jar; 赠送源代码:spring-oxm-5.3.10-sources.jar; 赠送Maven依赖信息文件:spring-oxm-5.3.10.pom; 包含翻译后的API文档:spring...

    spring-oxm-5.3.15-API文档-中文版.zip

    赠送jar包:spring-oxm-5.3.15.jar; 赠送原API文档:spring-oxm-5.3.15-javadoc.jar; 赠送源代码:spring-oxm-5.3.15-sources.jar; 赠送Maven依赖信息文件:spring-oxm-5.3.15.pom; 包含翻译后的API文档:spring...

    spring-oxm-5.3.15-API文档-中英对照版.zip

    赠送jar包:spring-oxm-5.3.15.jar; 赠送原API文档:spring-oxm-5.3.15-javadoc.jar; 赠送源代码:spring-oxm-5.3.15-sources.jar; 赠送Maven依赖信息文件:spring-oxm-5.3.15.pom; 包含翻译后的API文档:spring...

    spring-oxm-5.0.8.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-oxm-5.0.8.RELEASE.jar; 赠送原API文档:spring-oxm-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-oxm-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-oxm-5.0.8.RELEASE.pom;...

    spring-oxm-3.2.8.jar

    Spring oxm:Spring对于object/xml映射的支持,可以让JAVA与XML之间来回切换; spring-oxm是操作XML文件:生成,修改节点的完美第三方库

    spring-oxm-5.0.5.RELEASE-API文档-中文版.zip

    赠送jar包:spring-oxm-5.0.5.RELEASE.jar; 赠送原API文档:spring-oxm-5.0.5.RELEASE-javadoc.jar; 赠送源代码:spring-oxm-5.0.5.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-oxm-5.0.5.RELEASE.pom;...

    spring-oxm-5.2.15.RELEASE-API文档-中文版.zip

    赠送jar包:spring-oxm-5.2.15.RELEASE.jar; 赠送原API文档:spring-oxm-5.2.15.RELEASE-javadoc.jar; 赠送源代码:spring-oxm-5.2.15.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-oxm-5.2.15.RELEASE....

    spring-oxm-4.2.2.RELEASE-API文档-中文版.zip

    赠送jar包:spring-oxm-4.2.2.RELEASE.jar; 赠送原API文档:spring-oxm-4.2.2.RELEASE-javadoc.jar; 赠送源代码:spring-oxm-4.2.2.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-oxm-4.2.2.RELEASE.pom;...

    spring-oxm-5.0.8.RELEASE-API文档-中文版.zip

    赠送jar包:spring-oxm-5.0.8.RELEASE.jar; 赠送原API文档:spring-oxm-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-oxm-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-oxm-5.0.8.RELEASE.pom;...

    spring-oxm-5.2.15.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-oxm-5.2.15.RELEASE.jar; 赠送原API文档:spring-oxm-5.2.15.RELEASE-javadoc.jar; 赠送源代码:spring-oxm-5.2.15.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-oxm-5.2.15.RELEASE....

    spring-oxm-5.0.5.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-oxm-5.0.5.RELEASE.jar; 赠送原API文档:spring-oxm-5.0.5.RELEASE-javadoc.jar; 赠送源代码:spring-oxm-5.0.5.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-oxm-5.0.5.RELEASE.pom;...

    spring-oxm-4.2.2.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-oxm-4.2.2.RELEASE.jar; 赠送原API文档:spring-oxm-4.2.2.RELEASE-javadoc.jar; 赠送源代码:spring-oxm-4.2.2.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-oxm-4.2.2.RELEASE.pom;...

    spring-4.2.5-jar包

    没有上传doc文档,只有jar包

    php100视频教程全部ppt(1-112)合集整理

    php100视频教程全部ppt(1-112)合集整理

    spring-framework-5.1.x.zip

    执行gradlew :spring-oxm:compileTestJava,导入idea等在依赖下载完即可使用

    spring-oxm-4.2.8.RELEASE-API文档-中文版.zip

    赠送jar包:spring-oxm-4.2.8.RELEASE.jar; 赠送原API文档:spring-oxm-4.2.8.RELEASE-javadoc.jar; 赠送源代码:spring-oxm-4.2.8.RELEASE-sources.jar; 包含翻译后的API文档:spring-oxm-4.2.8.RELEASE-...

    spring-oxm-5.3.10-API文档-中英对照版.zip

    赠送jar包:spring-oxm-5.3.10.jar; 赠送原API文档:spring-oxm-5.3.10-javadoc.jar; 赠送源代码:spring-oxm-5.3.10-sources.jar; 赠送Maven依赖信息文件:spring-oxm-5.3.10.pom; 包含翻译后的API文档:spring...

Global site tag (gtag.js) - Google Analytics