Convert XML to Object in Java using JAX-B

In the following tutorial we will show you how you can convert XML to Object in Java using JAX-B.
####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 Model

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;
	}

	@Override
	public String toString() {
		return "Course{" + "id=" + id + ", name='" + name + '\'' + ", description='" + description + '\'' + '}';
	}
}

####Convert Object to XML
Here is how to convert xml to object in Java.

package com.sheting.basic.xml.jaxb;

import java.io.StringReader;

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

public class ConvertXmlToObject {

	public static void main(String... args) throws JAXBException {
		String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" + "<Course id=\"1\">\n"
				+ "    <name>JAX-B</name>\n"
				+ "    <description>Learning Java Architecture for XML Binding(JAX-B)</description>\n" + "</Course>";

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

		Course course = (Course) unmarshaller.unmarshal(new StringReader(xml));
		System.out.println(course);
	}
}

####Output

Course{id=1, name='JAX-B', description='Learning Java Architecture for XML Binding(JAX-B)'}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值