Java HTML / XML How to - Suppress XSI and xmlns in JAXB








Question

We would like to know how to Suppress XSI and xmlns in JAXB.

Answer

import java.util.ArrayList;
import java.util.List;
//from ww  w  .  j a  v  a2  s  .  c  o m
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;

public class Main {

  public static void main(String[] args) throws Exception {
    Root root = new Root();

    Book cDev = new Book();
    cDev.setName("C Development");
    root.getEmployeeDesiredSkills().add(cDev);

    Book perlDev = new Book();
    perlDev.setName("Perl Development");
    root.getEmployeeDesiredSkills().add(perlDev);

    JAXBContext jc = JAXBContext.newInstance(Root.class);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(root, System.out);
  }

  @XmlRootElement
  public static class Root {

    private List<Book> empBooks = new ArrayList<Book>();

    public List<Book> getEmployeeDesiredSkills() {
      return empBooks;
    }

    public void setEmployeeDesiredSkills(
        List<Book> employeeDesiredSkills) {
      this.empBooks = employeeDesiredSkills;
    }

  }

  public static class Book {

    private String name;

    @XmlValue
    public String getName() {
      return name;
    }

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

  }
}