什么是jaxb?
http://java.sun.com/xml/jaxb/about.html

主要能干什么?
当人们需要用java应用程序来访问数据库的时候,jdbc诞生了
当人们觉得频繁的jdbc操作很繁琐的时候,o/r mapping诞生了
当人们需要用java操作xml的时候,sax, dom诞生了
当人们觉得用dom操作xml很繁琐的时候, jaxb诞生了
jaxb---将xml与java对象绑定的sun规范.

使用:
jaxb的bin目录下有一个xcj.bat的批处理文件, 作用就是根据xml scheme文件比如xsd来生成java文件
sample.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
            jaxb:version="1.0" >
            <xsd:element name="registry" type="RegistryType"/>
           
            <xsd:complexType name="RegistryType">
                <xsd:sequence>
                    <xsd:element name="module" type="ModuleType"/>
                </xsd:sequence>
            </xsd:complexType>

<xsd:complexType name="ModuleType">
                <xsd:sequence>
                    <xsd:element name="serviceFactory" type="ServiceFactoryType"/>
                    <xsd:element name="serviceClientFactory" type="ServiceClientFactoryType"/>
                </xsd:sequence>
                <xsd:attribute name="name" type="xsd:string"/>
            </xsd:complexType>

<xsd:complexType name="ServiceFactoryType">
                <xsd:sequence>
                    <xsd:element name="service" type="ServiceType" minOccurs="0" maxOccurs="unbounded"/>
                </xsd:sequence>
                <xsd:attribute name="class" type="xsd:string">
                    <xsd:annotation>
                        <xsd:appinfo>
                            <jaxb:property name="clazz"/>
                        </xsd:appinfo>
                    </xsd:annotation>
                </xsd:attribute>
            </xsd:complexType>

<xsd:complexType name="ServiceType">
                <xsd:attribute name="id" type="xsd:string"/>
                <xsd:attribute name="class" type="xsd:string">
                    <xsd:annotation>
                        <xsd:appinfo>
                            <jaxb:property name="clazz"/>
                        </xsd:appinfo>
                    </xsd:annotation>
                </xsd:attribute>
            </xsd:complexType>

<xsd:complexType name="ServiceClientFactoryType">
                <xsd:sequence>
                    <xsd:element name="serviceClient" type="ServiceClientType"/>
                </xsd:sequence>
                <xsd:attribute name="class" type="xsd:string">
                    <xsd:annotation>
                        <xsd:appinfo>
                            <jaxb:property name="clazz"/>
                        </xsd:appinfo>
                    </xsd:annotation>
                </xsd:attribute>
            </xsd:complexType>

<xsd:complexType name="ServiceClientType">
                <xsd:sequence>
                    <xsd:element name="serviceRender" type="ServiceRenderType" minOccurs="0" maxOccurs="unbounded"/>
                </xsd:sequence>
                <xsd:attribute name="id" type="xsd:string"/>
                <xsd:attribute name="class" type="xsd:string">
                    <xsd:annotation>
                        <xsd:appinfo>
                            <jaxb:property name="clazz"/>
                        </xsd:appinfo>
                    </xsd:annotation>
                </xsd:attribute>
                <xsd:attribute name="renderFactory" type="xsd:string"/>
            </xsd:complexType>

<xsd:complexType name="ServiceRenderType">
                <xsd:attribute name="class" type="xsd:string">
                    <xsd:annotation>
                        <xsd:appinfo>
                            <jaxb:property name="clazz"/>
                        </xsd:appinfo>
                    </xsd:annotation>
                </xsd:attribute>
                <xsd:attribute name="id" type="xsd:string"/>
            </xsd:complexType>
</xsd:schema>
在cmd窗口下   xjc -p com.hairroot.jaxb sample.xsd
这个命令会在com/hairroot/jaxb的目录下生成几个interface, 并在com/hairroot/jaxb/impl下面生成各自的实现类,实现类看起来还是很复杂的。为什么这么复杂,原因就是需要考虑很多的事情,比如进行marshal的时候,如果尽可能的将输出跟输入xml看起来一致的xml, 而不是元素,属性的顺序弄得乱七八糟的。

注意:除了生成的java文件之外,还有一个jaxb.properties, 和ser。这两个东西是不能 丢的,否则会报错。

unmarshall的操作:
两种方式
    1)JAXBContext context = JAXBContext.newInstance("com.hairroot.jaxb");
          context.createUnmarshaller().unmarshal(new File("d:/hairroot/jaxb/sample.xml"));
    2)ObjectFactory factory = new ObjectFactory();
          factory.createUnmarshaller().unmarshal(new File("d:/hairroot/jaxb/sample.xml"));
    unmarshall返回的结果是一个object, 也就是根类型的一个实现类,可以强制转型了之后进行其它的操作。

marshall的操作:
同样有两种方式
    1)JAXBContext context = JAXBContext.newInstance("com.hairroot.jaxb");
          Object root = context.createUnmarshaller().unmarshal(new File("d:/hairroot/jaxb/sample.xml"));
          context.createMarshaller().marshal(root, System.out);
    2)ObjectFactory factory = new ObjectFactory();
          Object root = factory.createUnmarshaller().unmarshal(new File("d:/hairroot/jaxb/sample.xml"));
          factory.createMarshaller().marshal(root, System.out);

Validation的操作现在略;

如何将xsd:data类型进行格式化的输出:
    如果在schema中定义一个xsd:date类型,那么jaxb会将其转化为java.util.Calendar的类型,然而这样有个问题,如果 sample.xml中数据为2005-03-09, 那么进行marshal的操作后生成的xml为2005-03-09+08:00, 解决方法, 在scheme中
                        <xsd:element name="shipDate" minOccurs="0">
                            <xsd:simpleType>
                                <xsd:annotation>
                                    <xsd:appinfo>
                                        <jaxb:javaType name="java.sql.Date" printMethod="toString" parseMethod="valueOf"/>
                                    </xsd:appinfo>
                                </xsd:annotation>
                                <xsd:restriction base="xsd:date"/>
                            </xsd:simpleType>
                        </xsd:element>

并且还需要在xsd的namespace中加入
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
            jaxb:version="1.0" >
这样再进行xjc的时候不会报错,而且输入的date也就符合习惯了,如果不用
<jaxb:javaType name="java.sql.Date" printMethod="toString" parseMethod="valueOf"/>
则需要自己来写一个实现类,详细参考jaxb reference

如何处理java的一些保留字:
如果一个attribute的名字叫做class, 那么xjc产生的java中就有一个getClass的方法, 这与java.lang.Object 的getClass()相冲突,解决方法:
                <xsd:attribute name="class" type="xsd:string">
                    <xsd:annotation>
                        <xsd:appinfo>
                            <jaxb:property name="clazz"/>
                        </xsd:appinfo>
                    </xsd:annotation>
                </xsd:attribute>
可以用<jaxb:property name="clazz"/>来指定xjc生成的java的方法为getClazz()

jaxb1.0的问题:
目前我发现的主要问题是, attribute的输入的顺序跟schema中定义的顺序不一致,网上看到sun的工程师认为这不是一个很重要的问题,而且还认为attribute的顺序没有办法控制,这种借口简直好笑,如果能够做得更好为什么不去做?
推荐解决方法:
在xjc生成了java文件了之后,去看一下impl下面的每一个类的serializeAttributes()方法,调整这个方法的对应的 attribute的处理的上下顺序,就可以控制输出的xml的attribute出现的先后顺序,如此简单的事情,sun的工程师竟然说没有办法控制,当忽悠变成了一种时尚...

原文见 http://hairroot.blogchina.com/hairroot/899157.html

jaxb 的使用介绍[转载]相关推荐

  1. 源代码文档生成 Doxygen介绍(转载)

    源代码文档生成 Doxygen介绍(转载) 收藏 Doxygen介绍 一.Doxygen介绍 在项目开发过程中最重要的是如何和团队中其它成员沟通,如何在项目完成后减低维护成本,随着公司的人员流动,怎样 ...

  2. CCC认证介绍(转载)

    CCC认证介绍(转载) CCC认证介绍 http://www.quality-world.cn/guanli/367.html CCC为英文 ChinaCompulsoryCertification ...

  3. CE认证介绍(转载)

    2007 年 07 月 29 日, 星期日 --&gt CE认证介绍(转载) CE认证介绍 http://www.quality-world.cn/guanli/378.html " ...

  4. Html5 WebSocket 技术介绍(转载)

    WebSocket是html5规范新引入的功能,用于解决浏览器与后台服务器双向通讯的问题,使用WebSocket技术,后台可以随时向前端推送消息,以保证前后台状态统一,在传统的无状态HTTP协议中,这 ...

  5. Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式…)介绍--转载

    原文地址:http://www.blogways.net/blog/2013/06/02/jedis-demo.html redis是一个著名的key-value存储系统,而作为其官方推荐的java版 ...

  6. 注释驱动的 Spring cache 缓存介绍--转载

    概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...

  7. IE 8 Accelerator加速器开发介绍{转载}

    记录这篇博客的原因是因为我自己想要有一个快速的加速器,就是我经常在浏览网页的时候,看到有些网址,如果想要转过去的话,我必须手工复制到地址栏,然后回车一下.然后我就想,如果能直接通过一个快捷方式就太好了 ...

  8. [python爬虫] Selenium常见元素定位方法和操作的学习介绍(转载)

    转载地址:[python爬虫] Selenium常见元素定位方法和操作的学习介绍 一. 定位元素方法 官网地址:http://selenium-python.readthedocs.org/locat ...

  9. JSP中meta http-equiv=pragma content=no-cache属性ttp-equiv功能介绍转载

    http-equiv顾名思义,相当于http的文件头作用,它可以向浏览器传回一些有用的信息,以帮助正确和精确地显示网页内容,与之对应的属性值为content,content中的内容其实就是各个参数的变 ...

最新文章

  1. 一个互联网「打工人」的卑微一天
  2. 使用PYTORCH复现ALEXNET实现MNIST手写数字识别
  3. 【机器学习】降维技术-PCA
  4. 测试String, StringBuilder,StringBuffer 的数据,我居然发现这些了~~
  5. Golang笔记—面向对象编程
  6. Scikit-learn库中的数据预处理(一)
  7. 36000+开发者,一周投稿超 23000 篇,谁能笑傲群雄?| 第4周周榜揭晓
  8. MySql表以及数据导入导出
  9. Java学习系列(十七)Java面向对象之开发聊天工具
  10. Win10电脑如何找出隐藏的文件
  11. BZOJ2809:[Apio2012]dispatching——题解
  12. html select 事件 jquery,通过jquery触发select自身的change事件
  13. ExtJS新手调试过程
  14. MYSQL中SET NAMES UTF8的作用和内涵
  15. 使用tortoiseGit对GitHub添加公钥
  16. 判断鼠标不在控件上_【干货】Eprime编写鼠标点击型记忆再认实验
  17. bzoj1005 [HNOI2008]明明的烦恼(Prufer数列+高精度(wys算法。。。))
  18. jdk11 下载地址
  19. 复现贪吃蛇程序——吃食物增加长度(最后一篇)
  20. PS学习总结二:色彩

热门文章

  1. 【论文阅读】【综述】激光雷达-相机融合的道路检测方法
  2. 外汇交易的进场策略和出场策略
  3. 爱因斯坦出的一道测试题
  4. 良心推荐十款堪称神器的软件
  5. 力扣练习——31 有效的井字游戏
  6. 2021年A特种设备相关管理(锅炉压力容器压力管道)证考试及A特种设备相关管理(锅炉压力容器压力管道)复审模拟考试
  7. freeman链码,归一化链码,一阶差分链码,归一化一阶差分链码
  8. CCRC信息安全服务认证作用及部分注意事项
  9. html table设置行高_html5行高怎么设置 Excel做的表格怎么打印
  10. google内购In-App Billing