JAXB的用法及介绍

2024-03-16 17:18
文章标签 介绍 用法 jaxb

本文主要是介绍JAXB的用法及介绍,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、jaxb是什么  
    JAXB是Java Architecture for XML Binding的缩写。可以将一个Java对象转变成为XML格式,反之亦然。 
    我们把对象与关系数据库之间的映射称为ORM,其实也可以把对象与XML之间的映射称为OXM(Object XML Mapping)。原来JAXB是Java EE的一部分,在JDK1.6中,SUN将其放到了Java SE中,这也是SUN的一贯做法。JDK1.6中自带的这个JAXB版本是2.0,比起1.0(JSR 31)来,JAXB2(JSR 222)用JDK5的新特性Annotation来标识要作绑定的类和属性等,这就极大简化了开发的工作量。 
     二、jaxb应用模式  
    在JAVA EE 5\6中,jaxb可以很方便的与jax-rs、jax-ws集成,极大的简化了web service接口的开发工作量。 
     三、jaxb代码举例  
第一步:需要引入 javax.xml.bind.jar  
第二步:编写java bean; 
[java]  view plain copy
  1. package com.mkyong.core;  
  2.    
  3. import javax.xml.bind.annotation.XmlAttribute;  
  4. import javax.xml.bind.annotation.XmlElement;  
  5. import javax.xml.bind.annotation.XmlRootElement;  
  6.    
  7. @XmlRootElement  
  8. public class Customer {  
  9.    
  10.     String name;  
  11.     int age;  
  12.     int id;  
  13.    
  14.     public String getName() {  
  15.         return name;  
  16.     }  
  17.    
  18.     @XmlElement  
  19.     public void setName(String name) {  
  20.         this.name = name;  
  21.     }  
  22.    
  23.     public int getAge() {  
  24.         return age;  
  25.     }  
  26.    
  27.     @XmlElement  
  28.     public void setAge(int age) {  
  29.         this.age = age;  
  30.     }  
  31.    
  32.     public int getId() {  
  33.         return id;  
  34.     }  
  35.    
  36.     @XmlAttribute  
  37.     public void setId(int id) {  
  38.         this.id = id;  
  39.     }  
  40.    
  41. }  

第三步:main方法把java bean转化为xml字符串 
[java]  view plain copy
  1. package com.mkyong.core;  
  2.    
  3. import java.io.File;  
  4. import javax.xml.bind.JAXBContext;  
  5. import javax.xml.bind.JAXBException;  
  6. import javax.xml.bind.Marshaller;  
  7.    
  8. public class JAXBExample {  
  9.     public static void main(String[] args) {  
  10.    
  11.       Customer customer = new Customer();  
  12.       customer.setId(100);  
  13.       customer.setName("mkyong");  
  14.       customer.setAge(29);  
  15.    
  16.       try {  
  17.    
  18.         File file = new File("C:\\file.xml");  
  19.         JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);  
  20.         Marshaller jaxbMarshaller = jaxbContext.createMarshaller();  
  21.    
  22.         // output pretty printed  
  23.         jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);  
  24.    
  25.         jaxbMarshaller.marshal(customer, file);  
  26.         jaxbMarshaller.marshal(customer, System.out);  
  27.    
  28.           } catch (JAXBException e) {  
  29.         e.printStackTrace();  
  30.           }  
  31.    
  32.     }  
  33. }  

下面是输出: 
[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
  2. <customer id="100">  
  3.     <age>29</age>  
  4.     <name>mkyong</name>  
  5. </customer>  

四、jaxb开发常用  
    jdk提供了xjc工具可以使xsd自动生成相应的java bean,这大大提高了开发的效率。同时,我们也可以使用 trang.jar 把xml轻松转化为xsd。下面是使用的举例。 

    第一步:把数据库表映射为xml 

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <User u_id="1" u_name="moto" u_email="aaa@XXX.com"   
  3.     u_mood="今天放假了" u_state="online" u_mobile="12345678901"   
  4.     u_hometown="山西" u_job="IT软件工程师" u_avatar="w34353453543r53" />  
第二步:使用 trang.jar 转化为xsd文件。在命令行执行: 

[html]  view plain copy
  1. java -jar D:\lib\trang.jar user.xml user.xsd  
下面,是生成的User.java。 
[java]  view plain copy
  1. //  
  2. // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6   
  3. // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>   
  4. // Any modifications to this file will be lost upon recompilation of the source schema.   
  5. // Generated on: 2011.11.13 at 01:26:07 ���� CST   
  6. //  
  7.   
  8.   
  9. package com.moto.server.bean;  
  10.   
  11. import java.math.BigInteger;  
  12. import javax.xml.bind.annotation.XmlAccessType;  
  13. import javax.xml.bind.annotation.XmlAccessorType;  
  14. import javax.xml.bind.annotation.XmlAttribute;  
  15. import javax.xml.bind.annotation.XmlRootElement;  
  16. import javax.xml.bind.annotation.XmlSchemaType;  
  17. import javax.xml.bind.annotation.XmlType;  
  18. import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;  
  19. import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;  
  20.   
  21.   
  22. /** 
  23.  * <p>Java class for anonymous complex type. 
  24.  *  
  25.  * <p>The following schema fragment specifies the expected content contained within this class. 
  26.  *  
  27.  * <pre> 
  28.  * <complexType> 
  29.  *   <complexContent> 
  30.  *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
  31.  *       <attribute name="u_avatar" use="required" type="{http://www.w3.org/2001/XMLSchema}NCName" /> 
  32.  *       <attribute name="u_email" use="required" type="{http://www.w3.org/2001/XMLSchema}anySimpleType" /> 
  33.  *       <attribute name="u_hometown" use="required" type="{http://www.w3.org/2001/XMLSchema}NCName" /> 
  34.  *       <attribute name="u_id" use="required" type="{http://www.w3.org/2001/XMLSchema}integer" /> 
  35.  *       <attribute name="u_job" use="required" type="{http://www.w3.org/2001/XMLSchema}NCName" /> 
  36.  *       <attribute name="u_mobile" use="required" type="{http://www.w3.org/2001/XMLSchema}integer" /> 
  37.  *       <attribute name="u_mood" use="required" type="{http://www.w3.org/2001/XMLSchema}NCName" /> 
  38.  *       <attribute name="u_name" use="required" type="{http://www.w3.org/2001/XMLSchema}NCName" /> 
  39.  *       <attribute name="u_state" use="required" type="{http://www.w3.org/2001/XMLSchema}NCName" /> 
  40.  *     </restriction> 
  41.  *   </complexContent> 
  42.  * </complexType> 
  43.  * </pre> 
  44.  *  
  45.  *  
  46.  */  
  47. @XmlAccessorType(XmlAccessType.FIELD)  
  48. @XmlType(name = "")  
  49. @XmlRootElement(name = "User")  
  50. public class User {  
  51.   
  52.     @XmlAttribute(name = "u_avatar", required = true)  
  53.     @XmlJavaTypeAdapter(CollapsedStringAdapter.class)  
  54.     @XmlSchemaType(name = "NCName")  
  55.     protected String uAvatar;  
  56.     @XmlAttribute(name = "u_email", required = true)  
  57.     @XmlSchemaType(name = "anySimpleType")  
  58.     protected String uEmail;  
  59.     @XmlAttribute(name = "u_hometown", required = true)  
  60.     @XmlJavaTypeAdapter(CollapsedStringAdapter.class)  
  61.     @XmlSchemaType(name = "NCName")  
  62.     protected String uHometown;  
  63.     @XmlAttribute(name = "u_id", required = true)  
  64.     protected BigInteger uId;  
  65.     @XmlAttribute(name = "u_job", required = true)  
  66.     @XmlJavaTypeAdapter(CollapsedStringAdapter.class)  
  67.     @XmlSchemaType(name = "NCName")  
  68.     protected String uJob;  
  69.     @XmlAttribute(name = "u_mobile", required = true)  
  70.     protected BigInteger uMobile;  
  71.     @XmlAttribute(name = "u_mood", required = true)  
  72.     @XmlJavaTypeAdapter(CollapsedStringAdapter.class)  
  73.     @XmlSchemaType(name = "NCName")  
  74.     protected String uMood;  
  75.     @XmlAttribute(name = "u_name", required = true)  
  76.     @XmlJavaTypeAdapter(CollapsedStringAdapter.class)  
  77.     @XmlSchemaType(name = "NCName")  
  78.     protected String uName;  
  79.     @XmlAttribute(name = "u_state", required = true)  
  80.     @XmlJavaTypeAdapter(CollapsedStringAdapter.class)  
  81.     @XmlSchemaType(name = "NCName")  
  82.     protected String uState;  
  83.   
  84.     /** 
  85.      * Gets the value of the uAvatar property. 
  86.      *  
  87.      * @return 
  88.      *     possible object is 
  89.      *     {@link String } 
  90.      *      
  91.      */  
  92.     public String getUAvatar() {  
  93.         return uAvatar;  
  94.     }  
  95.   
  96.     /** 
  97.      * Sets the value of the uAvatar property. 
  98.      *  
  99.      * @param value 
  100.      *     allowed object is 
  101.      *     {@link String } 
  102.      *      
  103.      */  
  104.     public void setUAvatar(String value) {  
  105.         this.uAvatar = value;  
  106.     }  
  107.   
  108.     /** 
  109.      * Gets the value of the uEmail property. 
  110.      *  
  111.      * @return 
  112.      *     possible object is 
  113.      *     {@link String } 
  114.      *      
  115.      */  
  116.     public String getUEmail() {  
  117.         return uEmail;  
  118.     }  
  119.   
  120.     /** 
  121.      * Sets the value of the uEmail property. 
  122.      *  
  123.      * @param value 
  124.      *     allowed object is 
  125.      *     {@link String } 
  126.      *      
  127.      */  
  128.     public void setUEmail(String value) {  
  129.         this.uEmail = value;  
  130.     }  
  131.   
  132.     /** 
  133.      * Gets the value of the uHometown property. 
  134.      *  
  135.      * @return 
  136.      *     possible object is 
  137.      *     {@link String } 
  138.      *      
  139.      */  
  140.     public String getUHometown() {  
  141.         return uHometown;  
  142.     }  
  143.   
  144.     /** 
  145.      * Sets the value of the uHometown property. 
  146.      *  
  147.      * @param value 
  148.      *     allowed object is 
  149.      *     {@link String } 
  150.      *      
  151.      */  
  152.     public void setUHometown(String value) {  
  153.         this.uHometown = value;  
  154.     }  
  155.   
  156.     /** 
  157.      * Gets the value of the uId property. 
  158.      *  
  159.      * @return 
  160.      *     possible object is 
  161.      *     {@link BigInteger } 
  162.      *      
  163.      */  
  164.     public BigInteger getUId() {  
  165.         return uId;  
  166.     }  
  167.   
  168.     /** 
  169.      * Sets the value of the uId property. 
  170.      *  
  171.      * @param value 
  172.      *     allowed object is 
  173.      *     {@link BigInteger } 
  174.      *      
  175.      */  
  176.     public void setUId(BigInteger value) {  
  177.         this.uId = value;  
  178.     }  
  179.   
  180.     /** 
  181.      * Gets the value of the uJob property. 
  182.      *  
  183.      * @return 
  184.      *     possible object is 
  185.      *     {@link String } 
  186.      *      
  187.      */  
  188.     public String getUJob() {  
  189.         return uJob;  
  190.     }  
  191.   
  192.     /** 
  193.      * Sets the value of the uJob property. 
  194.      *  
  195.      * @param value 
  196.      *     allowed object is 
  197.      *     {@link String } 
  198.      *      
  199.      */  
  200.     public void setUJob(String value) {  
  201.         this.uJob = value;  
  202.     }  
  203.   
  204.     /** 
  205.      * Gets the value of the uMobile property. 
  206.      *  
  207.      * @return 
  208.      *     possible object is 
  209.      *     {@link BigInteger } 
  210.      *      
  211.      */  
  212.     public BigInteger getUMobile() {  
  213.         return uMobile;  
  214.     }  
  215.   
  216.     /** 
  217.      * Sets the value of the uMobile property. 
  218.      *  
  219.      * @param value 
  220.      *     allowed object is 
  221.      *     {@link BigInteger } 
  222.      *      
  223.      */  
  224.     public void setUMobile(BigInteger value) {  
  225.         this.uMobile = value;  
  226.     }  
  227.   
  228.     /** 
  229.      * Gets the value of the uMood property. 
  230.      *  
  231.      * @return 
  232.      *     possible object is 
  233.      *     {@link String } 
  234.      *      
  235.      */  
  236.     public String getUMood() {  
  237.         return uMood;  
  238.     }  
  239.   
  240.     /** 
  241.      * Sets the value of the uMood property. 
  242.      *  
  243.      * @param value 
  244.      *     allowed object is 
  245.      *     {@link String } 
  246.      *      
  247.      */  
  248.     public void setUMood(String value) {  
  249.         this.uMood = value;  
  250.     }  
  251.   
  252.     /** 
  253.      * Gets the value of the uName property. 
  254.      *  
  255.      * @return 
  256.      *     possible object is 
  257.      *     {@link String } 
  258.      *      
  259.      */  
  260.     public String getUName() {  
  261.         return uName;  
  262.     }  
  263.   
  264.     /** 
  265.      * Sets the value of the uName property. 
  266.      *  
  267.      * @param value 
  268.      *     allowed object is 
  269.      *     {@link String } 
  270.      *      
  271.      */  
  272.     public void setUName(String value) {  
  273.         this.uName = value;  
  274.     }  
  275.   
  276.     /** 
  277.      * Gets the value of the uState property. 
  278.      *  
  279.      * @return 
  280.      *     possible object is 
  281.      *     {@link String } 
  282.      *      
  283.      */  
  284.     public String getUState() {  
  285.         return uState;  
  286.     }  
  287.   
  288.     /** 
  289.      * Sets the value of the uState property. 
  290.      *  
  291.      * @param value 
  292.      *     allowed object is 
  293.      *     {@link String } 
  294.      *      
  295.      */  
  296.     public void setUState(String value) {  
  297.         this.uState = value;  
  298.     }  
  299.   
  300. }  

这篇关于JAXB的用法及介绍的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/816167

相关文章

Python库 Django 的简介、安装、用法入门教程

《Python库Django的简介、安装、用法入门教程》Django是Python最流行的Web框架之一,它帮助开发者快速、高效地构建功能强大的Web应用程序,接下来我们将从简介、安装到用法详解,... 目录一、Django 简介 二、Django 的安装教程 1. 创建虚拟环境2. 安装Django三、创

python中update()函数的用法和一些例子

《python中update()函数的用法和一些例子》update()方法是字典对象的方法,用于将一个字典中的键值对更新到另一个字典中,:本文主要介绍python中update()函数的用法和一些... 目录前言用法注意事项示例示例 1: 使用另一个字典来更新示例 2: 使用可迭代对象来更新示例 3: 使用

python连接sqlite3简单用法完整例子

《python连接sqlite3简单用法完整例子》SQLite3是一个内置的Python模块,可以通过Python的标准库轻松地使用,无需进行额外安装和配置,:本文主要介绍python连接sqli... 目录1. 连接到数据库2. 创建游标对象3. 创建表4. 插入数据5. 查询数据6. 更新数据7. 删除

Python中的sort()和sorted()用法示例解析

《Python中的sort()和sorted()用法示例解析》本文给大家介绍Python中list.sort()和sorted()的使用区别,详细介绍其参数功能及Timsort排序算法特性,涵盖自适应... 目录一、list.sort()参数说明常用内置函数基本用法示例自定义函数示例lambda表达式示例o

Python学习笔记之getattr和hasattr用法示例详解

《Python学习笔记之getattr和hasattr用法示例详解》在Python中,hasattr()、getattr()和setattr()是一组内置函数,用于对对象的属性进行操作和查询,这篇文章... 目录1.getattr用法详解1.1 基本作用1.2 示例1.3 原理2.hasattr用法详解2.

MySQL ORDER BY 语句常见用法、示例详解

《MySQLORDERBY语句常见用法、示例详解》ORDERBY是结构化查询语言(SQL)中的关键字,隶属于SELECT语句的子句结构,用于对查询结果集按指定列进行排序,本文给大家介绍MySQL... 目录mysql ORDER BY 语句详细说明1.基本语法2.排序方向详解3.多列排序4.常见用法示例5.

DNS查询的利器! linux的dig命令基本用法详解

《DNS查询的利器!linux的dig命令基本用法详解》dig命令可以查询各种类型DNS记录信息,下面我们将通过实际示例和dig命令常用参数来详细说明如何使用dig实用程序... dig(Domain Information Groper)是一款功能强大的 linux 命令行实用程序,通过查询名称服务器并输

Linux中的自定义协议+序列反序列化用法

《Linux中的自定义协议+序列反序列化用法》文章探讨网络程序在应用层的实现,涉及TCP协议的数据传输机制、结构化数据的序列化与反序列化方法,以及通过JSON和自定义协议构建网络计算器的思路,强调分层... 目录一,再次理解协议二,序列化和反序列化三,实现网络计算器3.1 日志文件3.2Socket.hpp

javaSE类和对象进阶用法举例详解

《javaSE类和对象进阶用法举例详解》JavaSE的面向对象编程是软件开发中的基石,它通过类和对象的概念,实现了代码的模块化、可复用性和灵活性,:本文主要介绍javaSE类和对象进阶用法的相关资... 目录前言一、封装1.访问限定符2.包2.1包的概念2.2导入包2.3自定义包2.4常见的包二、stati

setsid 命令工作原理和使用案例介绍

《setsid命令工作原理和使用案例介绍》setsid命令在Linux中创建独立会话,使进程脱离终端运行,适用于守护进程和后台任务,通过重定向输出和确保权限,可有效管理长时间运行的进程,本文给大家介... 目录setsid 命令介绍和使用案例基本介绍基本语法主要特点命令参数使用案例1. 在后台运行命令2.