JAXB Example with XML Namespace

Connect with

JAXB Example with XML NamespaceLearn different aspect of JAXB Example with XML Namespace. Different configuration for XML namespace. How do you marshal and un-marshal in case of XML namespace.

1. JAXB Example with XML Namespace

Masrshaling and unmarshaling of simple XML document is very easy. When we talked about marshaling and un-marshaling of XML namespace document then its tricky so learn tricks for XML namespace.

2. POJO Class for namespace

File: Address.java

package com.mysoftkey.jaxb.namespace;

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

@XmlRootElement()
@XmlAccessorType(XmlAccessType.FIELD)
public class Address {

  private String street;
  private String city;
  private String state;
	
  /**
   * default constructor needed as I have added 
   * parameterized constructor to populate values.
   */
  public Address(){
  }
	
 /**
  * @param street
  * @param city
  * @param state
  */
  public Address(String street, String city, String state){
	this.street = street;
	this.city = city;
	this.state = state;
  }
	
  public String getStreet() {
	return street;
  }
  
  public void setStreet(String street) {
	this.street = street;
  }
  public String getCity() {
	return city; 
  }
  public void setCity(String city) {
	this.city = city;
  }
  public String getState() {
	return state;
  }
  public void setState(String state) {
	this.state = state;
  }
}

File: Customre.java

package com.mysoftkey.jaxb.namespace;

import java.util.ArrayList;
import java.util.List;

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.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * @author Ranjeet.Jha
 * 
 */
@XmlRootElement(namespace = "http://mysoftkey.com/jaxb")
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {

  private String name;
  private int age;
  private int id;
	
  @XmlElementWrapper(name = "customerAddress")
  @XmlElement(name = "address")
  private List
addresses; public String getName() { return name; } public Customer() { } public Customer(int id, String name, int age) { this.id=id; this.name=name; this.age=age; } /** * this method add the address into customer. * * @param address */ public void addAddrss(Address address) { if (addresses == null || addresses.size() == 0) { addresses = new ArrayList
(); } addresses.add(address); } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getId() { return id; } //@XmlAttribute public void setId(int id) { this.id = id; } }

3. Java Example with XML namespace

File:JAXBExampleWithXMLNamespace.java

package com.mysoftkey.jaxb.namespace;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

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

/**
 * @author Ranjeet.Jha
 *
 */
public class File:JAXBExampleWithXMLNamespace {

  public static final String CUSTOMER_FILE = "./docs/customer.xml";
	
   public static void main(String[] args)  {

    try {
      marshalCustomer();
     //getCustomerByreadingXML();
    } catch (Exception e) {
  	e.printStackTrace();
    }
		
  }

  private static void marshalCustomer() {
	
    try {
	// create customer object and populate values.
	Customer customer = new Customer(1, "Anushka", 12);
		
	// add address into customer in addresses property
	customer.addAddrss(new Address("Street no1 ", "New Delhi", "Delhi"));
	customer.addAddrss(new Address("Street no 2 ", "New Delhi", "Delhi"));
		
	File file = new File(CUSTOMER_FILE);
	JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
	Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

	// output pretty printed
	jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

	jaxbMarshaller.marshal(customer, file);
	jaxbMarshaller.marshal(customer, System.out);
			
	
    } catch (JAXBException e) {
	e.printStackTrace();
    }
 }

  private static Customer getCustomerByreadingXML() throws JAXBException, FileNotFoundException {
		
	JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
		
	// get variables from our xml file, created before
	System.out.println("Output from our XML File: ");
	Unmarshaller um = jaxbContext.createUnmarshaller();
	Customer cust = (Customer) um.unmarshal(new FileReader(CUSTOMER_FILE));
	System.out.println(cust.getName());
	return cust;
  }

}

Output

4. Output with Default Namespace in XML

configuration:

@XmlRootElement(namespace = "http://mysoftkey.com/jaxb") // , name="mycustomer"
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {

  private String name;
  private int age;
  private int id;
	
  @XmlElementWrapper(name = "customerAddress")
  @XmlElement(name = "address")
  private List <address> addresses;
// setter and getter


    Anushka
    12
    1
    
        
Street no1 New Delhi Delhi
Street no 2 New Delhi Delhi

Configuration:

@XmlRootElement(namespace = "http://mysoftkey.com/jaxb" , name="mycustomer")
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {

Output:



    Anushka
    12
    1
    
        
Street no1 New Delhi Delhi
Street no 2 New Delhi Delhi

Here, provide namespace in java class as mycustomer, which has included in xml at line no 2.

5. JAXB example Without namespace

configuration as follows, here , commented parameter provided in @XmlRootElement

@XmlRootElement(/*namespace = "http://mysoftkey.com/jaxb" , name="mycustomer"*/)
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {

	private String name;
	private int age;
	private int id;
	
	@XmlElementWrapper(name = "customerAddress")
 @XmlElement(name = "address")
	private List
addresses;

output XML



    Anushka
    12
    1
    
        
Street no1 New Delhi Delhi
Street no 2 New Delhi Delhi

Your comment is welcome to improve this post. Happy learning! 🙂

6. Reference

oracle.com
I hope you enjoyed this post about JAXB example with XML namespace, visit Core Java tutorial for more blog post.

Your comment is welcome to improve this post. Happy Learning! 🙂


Connect with

Leave a Comment

Your email address will not be published. Required fields are marked *