Question

Its easier to explain with an example,

Sample XML:

<root>  
 <company>
        <name>xyz</name>
        <employees>
           <employeeref>emp1</employeeref>
           <employeeref>emp2</employeeref>
        </employees>   
 </company>

      <employee id="emp1">
         <name>a</name>
         <age>12</age>   </employee>

      <employee id="emp2">
         <name>b</name>
         <age>24</age>   </employee>

      <employee id="emp3">
         <name>c</name>
         <age>36</age>   
      </employee> 
</root>

This would need Company.java and Employee.java.

My question is, How do i bind the xml to java objects using Jaxb annotations?

Was it helpful?

Solution

You can use @XmlID in combination with @XmlIDREF

Employee

On the Employee class you need to annotate which field/property is going to be the "key" for the object. In JAXB this is done with the @XmlID annotation.

@XmlAccessorType(XmlAccessType.FIELD)
public class Employee {

    @XmlAttribute
    @XmlID
    private String id;
}

Company

In the Company class we will indicate that we want to marshal instances of Employee as a reference to the real object. This is done using the @XmlIDREF annotation.

@XmlAccesorType(XmlAccessType.FIELD)
public class Company {

    @XmlElementWrapper
    @XmlElement(name="elementref")
    @XmlIDREF
    private List<Employee> employees;
}

Root

In order for @XmlIDREF to work the object must mapped somewhere else with an @XmlElement annotation. In this example this happes in the Root class.

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

    private Company company;

    @XmlElement(name="employee")
    private List<Employee> employees;

}

For More Information

You can read more about @XmlID and @XmlIDREF on my blog:

OTHER TIPS

I think you need following class, here is sample, you can do it with other ways

Root    One class for Root
    | -- Company  Company class
    |  |
    |  | employess(List)  one more class for employess collection
    |         
    |         
    employee (collection) one class for employee

EmployeeData

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "root")
public class EmployeeData {
    @XmlElement
    private Company company;
    private List<Employee> employee; 
// getter/setter
}

Company

@XmlAccessorType(XmlAccessType.FIELD)
public class Company {
    @XmlElement(required = true)
    private String name;
    @XmlElement(required = true)
    private Employees employees;
//...// getter/setter
}

Employee

@XmlAccessorType(XmlAccessType.FIELD)
public class Employee {
        @XmlElement
    private String name;
    private int age;
    @XmlAttribute(name = "id")
    private String id;
///// getter/setter
}

Employees

@XmlAccessorType(XmlAccessType.FIELD)
public class Employees {
        private List<String> employeeref;

// getter/setter }

Java classes can be structured in such a way that they form a tree hierarchy as described in the XML file. Annotations are used in the Java classes to describe this structure. Here is a list of some annotations used:

  • @XmlRootElement : Maps a class or an enum type to an XML element.
  • @XmlAttribute : Maps a JavaBean property to a XML attribute.
  • @XmlElement : Maps a JavaBean property to a XML element derived from property name

The Employee class can be like that:

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Employee {
    private String id;

    private String name;

    private String age;

    public String getId() {
        return id;
    }
    @XmlAttribute
    public void setId(String id) {
        this.id = id;
    }

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

    public String getAge() {
        return age;
    }
    @XmlElement
    public void setAge(String age) {
        this.age = age;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top