Convert a Java Object to XML using JAXB

本文介绍如何使用JAX-B将Java对象转换为XML格式。通过示例解释了@XmlRootElement、@XmlAccessorType等注解的作用,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In the following tutorial we will show you how you can convert a Java Object to XML.

Annotations Explained

First we need to define our model that will be converted to XML representation. Here are a couple of annotations with their explanations what they do.

  • @XmlRootElement : This annotation defines the root element. It is required by JAX-B, without this, the object cannot be converted into xml.
  • @XmlAccessorType : This tells JAX-B witch access level is used.
    • NONE: Indicates(暗示,标识) that JAX-B will not convert any properties.
    • FIELD: Indicates that JAX-B will search for annotations on field level.
    • PROPERTY: Indicates that JAX-B will search for annotations on property level.
    • PUBLIC_MEMBER (default): Indicates that JAX-B will search for public members.
  • @XmlAttribute: This tels JAX-B to convert the property or field into an XML attribute.
  • @XmlElement: This tells JAX-B to convert the property or field into an XML element. This is not required. By default JAX-B uses every public non-transient member into an element if the class is annotated with @XmlRootElement.
The Mode
package com.sheting.basic.xml.jaxb;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Course")
@XmlAccessorType(XmlAccessType.FIELD)
public class Course {

    @XmlAttribute
    private long id;
    @XmlElement
    private String name;
    private String description;

    /**
     * Default no-args constructor needed by JAX-B
     */
    public Course() {
    }

    public Course(long id, String name, String description) {
        this.id = id;
        this.name = name;
        this.description = description;
    }

    public long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}
Convert Object to XML

Here is how to marshal the java object to xml.

package com.sheting.basic.xml.jaxb;

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

public class ConvertObjectToXml {
    public static void main(String... args) throws JAXBException {
        // create the object
        Course course = new Course(1, "JAX-B", "Learning Java Architecture for XML Binding(JAX-B)");

        // create jaxb context
        JAXBContext jaxbContext = JAXBContext.newInstance(Course.class);

        // create jaxb marshaller
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // pretty print xml
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        // marshal object
        jaxbMarshaller.marshal(course, System.out);
    }
}
Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Course id="1">
    <name>JAX-B</name>
    <description>Learning Java Architecture for XML Binding(JAX-B)</description>
</Course>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值