Java to XML, XML to Java (Marshalling and Unmarshalling)

Wednesday, July 13th, 2011

Introduction

JDK6 and JAXB2.x (which comes with JDK6) make marshalling Java to XML and unmarshalling XML to Java a snap, almost trivial.

Example

Java to XML

package foo.bar;

import java.math.BigDecimal;

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

public class JavaToXML {
	public static void main(String[] args) throws Exception {
		JAXBContext context = JAXBContext.newInstance(Product.class);

		Marshaller m = context.createMarshaller();
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

		Product object = new Product();
		object.setCode("WI1");
		object.setName("Widget Number One");
		object.setPrice(BigDecimal.valueOf(300.00));

		m.marshal(object, System.out);
	}
}

XML to Java

package foo.bar;

import java.io.File;

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

public class XMLToJava {

	public static void main(String[] args) {
		try {
			JAXBContext jc = JAXBContext.newInstance(Product.class);
			Unmarshaller u = jc.createUnmarshaller();

			File f = new File("product.xml");
			Product product = (Product) u.unmarshal(f);

			System.out.println(product.getCode());
			System.out.println(product.getName());
			System.out.println(product.getPrice());
		} catch (JAXBException e) {
			e.printStackTrace();
		}
	}
}

product.java

Note: the @XmlRootElement is vital here!

package foo.bar;

import java.math.BigDecimal;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Product {
	private String code;
	private String name;
	private BigDecimal price;

	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public BigDecimal getPrice() {
		return price;
	}
	public void setPrice(BigDecimal price) {
		this.price = price;
	}
}

product.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<product>
    <code>WI1</code>
    <name>Widget Number One</name>
    <price>300.0</price>
</product>

You can leave a response, or trackback from your own site.

Tags: , ,
Posted in: Examples



8 Responses to “Java to XML, XML to Java (Marshalling and Unmarshalling)”

  1. Premchand Says:

    June 11th, 2012 at 11:45 am

    this program is not working…..
    it is giving the following errors:->
    1. Exception in thread “main” javax.xml.bind.JAXBException
    2. .
    3. Caused by: java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory

  2. lawrence Says:

    June 17th, 2012 at 9:14 am

    Are you using Java 6 or higher?

  3. senthil Says:

    January 25th, 2013 at 3:05 am

    very good example for java marshalling and unmarshalling

  4. senthil Says:

    January 25th, 2013 at 3:19 am

    @Premchand
    verify the JDK install. After Jdk 1.6 there is no extra jaxb libraries are required , because JAXB is bundled in it

  5. priyabrat Says:

    April 2nd, 2013 at 10:13 am

    Many Thanks,It’s working properly..

  6. Devendra Says:

    April 7th, 2014 at 10:43 am

    What if I have multiple instances of the product and want to generate xml for same.

  7. How do I add XML list elements to DefaultListModel? Says:

    December 19th, 2014 at 4:01 pm

    […] Thanked 0 Times in 0 Posts EDIT: I found this link but I don't know how to add the parts to the list once I unmarshal. Java to XML, XML to Java (Marshalling and Unmarshalling) | lstierneyltd […]

  8. Sridhar Babu Says:

    September 5th, 2017 at 8:33 pm

    How can we implement for PRODUCTS, In the Example its have only one Product ,if its have more thank 1 project , how can we implement

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>