Rajesh
Rajesh

Reputation: 213

issues while Unmarshalling using JaxB

I have the following XML file to unmarshall

<root>
  <emp>Google</emp>
  <emp>Yahoo</emp>
  <xyz>random</xyz>
</root>

And i have used annotations in the following way,

@XmlRootElement(name = "root")
@XmlAccessorType(XmlAccessType.FIELD)
public class abc {

   @XmlElement(name = "emp")
   private String emp1;
   @XmlElement(name = "emp")
   private String emp2;
   @XmlElement(name = "xyz")
   private String xyz;
   // added getters and setters for these fields
}

My problem is while i'm trying to get

   obj.getEmp1(); // result is Yahoo instead of Google
   obj.getEmp2(); // result is null.

Kindly clarify me, what am i doing wrong?

Upvotes: 3

Views: 148

Answers (3)

bdoughan
bdoughan

Reputation: 148977

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

The standard JAXB (JSR-222) annotations do not support mapping 2 different properties to the same XML element.

You could use EclipseLink JAXB (MOXy)'s @XmlPath extension for this use case.

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement(name = "root")
@XmlAccessorType(XmlAccessType.FIELD)
public class abc {

   @XmlPath("emp[1]/text()")
   private String emp1;
   @XmlPath("emp[2]/text()")
   private String emp2;
   @XmlElement(name = "xyz")
   private String xyz;

   // added getters and setters for these fields
}

For More Information

Upvotes: 4

mavroprovato
mavroprovato

Reputation: 8352

If for whatever reason you cannot use MOXy, another solution would be to map the emp element as a list

@XmlRootElement(name = "root")
@XmlAccessorType(XmlAccessType.FIELD)
public class abc {
   @XmlElement(name = "emp")
   private List<String> emp;
   @XmlElement(name = "xyz")
   private String xyz;
   // added getters and setters for these fields
}

And then use the following code to get the values:

obj.getEmp().get(0);
obj.getEmp().get(1);

But Blaise's solution is more elegant


You could have a String[] field and have your current accessor methods access the String[].

import javax.xml.bind.annotation.*;

@XmlRootElement(name = "root")
@XmlAccessorType(XmlAccessType.FIELD)
public class abc {

    private String[] emp = new String[2];
    private String xyz;

    public String getEmp1() {
        return emp[0];
    }

    public void setEmp1(String emp1) {
        this.emp[0] = emp1;
    }

    public String getEmp2() {
        return emp[1];
    }

    public void setEmp2(String emp2) {
        this.emp[1] = emp2;
    }

    public String getXyz() {
        return xyz;
    }

    public void setXyz(String xyz) {
        this.xyz = xyz;
    }

}

Upvotes: 2

FuryFart
FuryFart

Reputation: 2393

This might work.

<root>
  <emp1>Google</emp1>
  <emp2>Yahoo</emp2>
  <xyz>random</xyz>
</root>

@XmlRootElement(name = "root")
@XmlAccessorType(XmlAccessType.FIELD)
public class abc {

   @XmlElement(name = "emp1")
   private String emp1;
   @XmlElement(name = "emp2")
   private String emp2;
   @XmlElement(name = "xyz")
   private String xyz;
}

Upvotes: 0

Related Questions