jaxb教程_JAXB示例教程

jaxb教程

Welcome to JAXB Example Tutorial. Java Architecture for XML Binding (JAXB) provides API for converting Object to XML and XML to Object easily. JAXB was developed as a separate project but it was used widely and finally became part of JDK in Java 6.

欢迎使用JAXB示例教程。 XML绑定Java体系结构JAXB )提供了API,可以轻松地将Object转换为XML和将XML转换为Object。 JAXB是作为一个单独的项目开发的,但得到了广泛的使用,最终成为Java 6中JDK的一部分。

JAXB教程 (JAXB Tutorial)

This tutorial is based on Java 7 and current JAXB version 2.0.

本教程基于Java 7和当前的JAXB 2.0版。

JAXB Marshalling: Converting a Java Object to XML.

JAXB编组 :将Java对象转换为XML。

JAXB Unmarhsalling: Converting XML to Java Object.

JAXB Unmarhsalling :将XML转换为Java对象。

Using JAXB is very easy and it uses java annotations. We need to annotate Java Object to provide instructions for XML creation and then we have to create Marshaller to convert Object to XML.

使用JAXB非常容易,并且使用Java批注 。 我们需要注释Java Object以提供XML创建的说明,然后我们必须创建Marshaller将Object转换为XML。

Unmarshaller is used to convert XML to java Object.

Unmarshaller用于将XML转换为Java Object。

JAXBContext is the entry point for JAXB and provides methods to get marshaller and unmarshaller object.

JAXBContext是JAXB的入口点,并提供了获取编组器和解组器对象的方法。

Some basic and useful JAXB annotations are:

一些基本和有用的JAXB注释是:

  • @XmlRootElement: This is a must have annotation for the Object to be used in JAXB. It defines the root element for the XML content.

    @XmlRootElement :这是要在JAXB中使用的对象必须具有的注释。 它定义了XML内容的根元素。
  • @XmlType: It maps the class to the XML schema type. We can use it for ordering the elements in the XML.

    @XmlType :它将类映射到XML模式类型。 我们可以使用它来排序XML中的元素。
  • @XmlTransient: This will make sure that the Object property is not written to the XML.

    @XmlTransient :这将确保不将Object属性写入XML。
  • @XmlAttribute: This will create the Object property as attribute.

    @XmlAttribute:这将创建Object属性作为属性。
  • @XmlElement(name = “abc”): This will create the element with name “abc”

    @XmlElement(name =“ abc”):这将创建名称为“ abc”的元素

There are some other JAXB annotation that you can learn from JAXB Official Site.

您还可以从JAXB官方网站学习其他JAXB批注。

JAXB示例 (JAXB Example)

Let’s run a simple JAXB example program to demonstrate the use of JAXB marshalling and unmarshalling.

让我们运行一个简单的JAXB示例程序,以演示JAXB编组和反编组的用法。

package com.journaldev.xml.jaxb;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "Emp")
@XmlType(propOrder = {"name", "age", "role", "gender"})
public class Employee {

    private int id;

    private String gender;

    private int age;
    private String name;
    private String role;

    private String password;


    @XmlTransient
    public String getPassword() {
        return password;
    }


    public void setPassword(String password) {
        this.password = password;
    }


    @XmlAttribute
    public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }


    public int getAge() {
        return age;
    }


    public void setAge(int age) {
        this.age = age;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    @XmlElement(name = "gen")
    public String getGender() {
        return gender;
    }


    public void setGender(String gender) {
        this.gender = gender;
    }


    public String getRole() {
        return role;
    }


    public void setRole(String role) {
        this.role = role;
    }


    @Override
    public String toString() {
        return "ID = " + id + " NAME=" + name + " AGE=" + age + " GENDER=" + gender + " ROLE=" +
                role + " PASSWORD=" + password;
    }

}

Employee is a normal java bean with private fields and their getters and setters. Notice the use of JAXB annotations to define root element, element name, elements order and transient property.

Employee是一个普通的Java Bean,具有私有字段及其获取器和设置器。 注意,使用JAXB批注定义根元素,元素名称,元素顺序和瞬态属性。

Here is a test JAXB example program showing JAXB Marshalling and JAXB Unmarshalling.

这是一个测试JAXB示例程序,显示了JAXB编组JAXB 编组

package com.journaldev.xml.jaxb;


import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;


public class JAXBExample {

    private static final String FILE_NAME = "jaxb-emp.xml";

    public static void main(String[] args) {
        Employee emp = new Employee();
        emp.setId(1);
        emp.setAge(25);
        emp.setName("Pankaj");
        emp.setGender("Male");
        emp.setRole("Developer");
        emp.setPassword("sensitive");

        jaxbObjectToXML(emp);

        Employee empFromFile = jaxbXMLToObject();
        System.out.println(empFromFile.toString());
    }


    private static Employee jaxbXMLToObject() {
        try {
            JAXBContext context = JAXBContext.newInstance(Employee.class);
            Unmarshaller un = context.createUnmarshaller();
            Employee emp = (Employee) un.unmarshal(new File(FILE_NAME));
            return emp;
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return null;
    }


    private static void jaxbObjectToXML(Employee emp) {

        try {
            JAXBContext context = JAXBContext.newInstance(Employee.class);
            Marshaller m = context.createMarshaller();
            //for pretty-print XML in JAXB
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

            // Write to System.out for debugging
            // m.marshal(emp, System.out);

            // Write to File
            m.marshal(emp, new File(FILE_NAME));
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }

}

Above program creates following jaxb-emp.xml file.

上面的程序创建以下jaxb-emp.xml文件。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Emp id="1">
    <name>Pankaj</name>
    <age>25</age>
    <role>Developer</role>
    <gen>Male</gen>
</Emp>

Check that XML file don’t have password field and we get following output when same XML file is unmarshalled to Object.

检查XML文件没有密码字段,当同一XML文件解组到Object时,我们得到以下输出。

ID = 1 NAME=Pankaj AGE=25 GENDER=Male ROLE=Developer PASSWORD=null

That’s all for JAXB example tutorial, as you can see that it’s very easy to use.

这就是JAXB示例教程的全部内容,您可以看到它非常易于使用。

翻译自: https://www.journaldev.com/1234/jaxb-example-tutorial

jaxb教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值