Questions tagged [jaxb]

Java Architecture for XML Binding is the Java standard for working with XML as domain objects. It provides an easy mechanism for mapping Java classes to XML representations.

Java Architecture for XML Binding (JAXB) is the standard (JSR-222) for working with as domain objects. It provides an easy to use mechanism for mapping Java classes to XML representations. An implementation is included as part of the Java SE 6 API. There are several implementations available including Metro JAXB (the reference implementation), EclipseLink MOXy, and JaxMe (retired).

JAXB and Java EE

JAXB is the standard binding layer for the following specifications:

John Doe says "Hello World" example

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.StringReader;

public class JohnDoeSaysHello {
  public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Person.class);

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    String xml = "<person><name>John Doe</name></person>";
    Person person = (Person) unmarshaller.unmarshal(new StringReader(xml));
    System.out.println("I'm the person, " 
            + person.getName() 
            + ", unmarshalled from XML using a StringReader, saying, \"Hello World!\"");

    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(person, System.out);
  }

  @XmlRootElement
  public static class Person {
    private String name;

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

Documentation

11092 questions
1219
votes
45 answers

How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException

I have some code that uses JAXB API classes which have been provided as a part of the JDK in Java 6/7/8. When I run the same code with Java 9, at runtime I get errors indicating that JAXB classes can not be found. The JAXB classes have been…
Andy Guibert
  • 41,446
  • 8
  • 38
  • 61
347
votes
1 answer

Java 11 package javax.xml.bind does not exist

I'm trying to deserialize XML data into a Java content tree using JAXB, validating the XML data as it is unmarshalled: try { JAXBContext context = JAXBContext.newInstance("com.acme.foo"); Unmarshaller unmarshaller =…
Boris
  • 22,667
  • 16
  • 50
  • 71
245
votes
17 answers

No @XmlRootElement generated by JAXB

I'm trying to generate Java classes from the FpML (Finanial Products Markup Language) version 4.5. A ton of code is generated, but I cannot use it. Trying to serialize a simple document I get this: javax.xml.bind.MarshalException - with linked…
robinr
  • 4,376
  • 2
  • 20
  • 18
194
votes
6 answers

Use JAXB to create Object from XML String

How can I use the below code to unmarshal a XML string an map it to the JAXB object below? JAXBContext jaxbContext = JAXBContext.newInstance(Person.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); Person person = (Person)…
c12
  • 9,557
  • 48
  • 157
  • 253
147
votes
18 answers

Jaxb, Class has two properties of the same name

With Jaxb (jaxb-impl-2.1.12), UI try to read an XML file Only a few element in the XML file are interesting, so I would like to skip most of the elements. The XML I'm trying to read: